Ruby - convert string to date
For Ruby 1.9.2:
require 'date' # If not already required. If in Rails then you don't need this line).
puts DateTime.parse("2011-06-02T23:59:59+05:30").to_date.to_s
Ruby String to Date Conversion
What is wrong with Date.parse
method?
str = "Tue, 10 Aug 2010 01:20:19 -0400 (EDT)"
date = Date.parse str
=> #<Date: 4910837/2,0,2299161>
puts date
2010-08-10
It seems to work.
The only problem here is time zone. If you want date in UTC time zone, then it is better to use Time
object, suppose we have string:
str = "Tue, 10 Aug 2010 01:20:19 +0400"
puts Date.parse str
2010-08-10
puts Date.parse(Time.parse(str).utc.to_s)
2010-08-09
I couldn't find simpler method to convert Time
to Date
.
Ruby String to Date Conversion
What is wrong with Date.parse
method?
str = "Tue, 10 Aug 2010 01:20:19 -0400 (EDT)"
date = Date.parse str
=> #<Date: 4910837/2,0,2299161>
puts date
2010-08-10
It seems to work.
The only problem here is time zone. If you want date in UTC time zone, then it is better to use Time
object, suppose we have string:
str = "Tue, 10 Aug 2010 01:20:19 +0400"
puts Date.parse str
2010-08-10
puts Date.parse(Time.parse(str).utc.to_s)
2010-08-09
I couldn't find simpler method to convert Time
to Date
.
convert String to DateTime
DateTime.strptime allows you to specify the format and convert a String to a DateTime.
Ruby - Convert a string to Date
if you want date object
date_string = "25/04/14"
date = Date.strptime(date_string, "%d/%m/%y")
if you want 25-04-2014
format string
new_format = date.strftime("%d-%m-%Y")
Convert string to datetime ruby on rails
You can use DateTime to parse the date from a specific format.
if the format you are looking to parse is "03/28/2018 1:46 AM" then you can do this.
date = DateTime.strptime('03/28/2018 1:46 AM', '%m/%d/%Y %I:%M %p')
# date to ISO 8601
puts date.to_time
# output: 2018-03-28 07:16:00 +0530
puts date.strftime("%m/%d/%Y")
# output: 03/28/2018
Date formats:
Date (Year, Month, Day):
%Y - Year with century (can be negative, 4 digits at least)
-0001, 0000, 1995, 2009, 14292, etc.
%m - Month of the year, zero-padded (01..12)
%_m blank-padded ( 1..12)
%-m no-padded (1..12)
%d - Day of the month, zero-padded (01..31)
%-d no-padded (1..31)
Time (Hour, Minute, Second, Subsecond):
%H - Hour of the day, 24-hour clock, zero-padded (00..23)
%k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
%I - Hour of the day, 12-hour clock, zero-padded (01..12)
%l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
%P - Meridian indicator, lowercase (``am'' or ``pm'')
%p - Meridian indicator, uppercase (``AM'' or ``PM'')
%M - Minute of the hour (00..59)
You can refer to all formats here.
Converting string to date with Ruby
Remove the period from my_string
and from the date pattern.
Date.strptime(my_string.sub('.', ''), '%d %b %Y')
That's assuming you have at most one dot in my_string
. If there may be several, use gsub
.
Date.strptime(my_string.gsub('.', ''), '%d %b %Y')
Convert string to date time in Ruby
Here you go:
DateTime.strptime("03/30/2021 4:30 PM", "%m/%d/%Y %H:%M %p") # => Tue, 30 Mar 2021 16:30:00 +0000
And here is the strptime
method, https://ruby-doc.org/stdlib/libdoc/date/rdoc/DateTime.html
Ruby: convert string to date
You could also use Date.strptime
:
Date.strptime("{ 2009, 4, 15 }", "{ %Y, %m, %d }")
Related Topics
Monkey Patching Devise (Or Any Rails Gem)
Get Parent Directory of Current Directory in Ruby
Rvm Ruby 1.9.1 Install Can't Locate Zlib But Its Runtime and Dev Library Are There
A Copy of Xxx Has Been Removed from the Module Tree But Is Still Active
Getting Rid of Ruby Gems That Won't Die
Why Does Ruby Have Trueclass and Falseclass Instead of a Single Boolean Class
Ruby on Rails "Invalid Byte Sequence in Utf-8" Due to Bot
Gem Install Pg Doesn't Work on Osx Lion
How to Add Information to an Exception Message in Ruby
Ruby String to Date Conversion
Active Record - Find Records Which Were Created_At Before Today
Ruby CSV - Get Current Line/Row Number
Get Current Ruby Process Memory Usage
Error Installing Nokogiri: Failed to Build Gem Native Extension & Libiconv Is Missing (Osx)
Rails Forms for Has_Many Through Association with Additional Attributes
Add Nullable Foreign Key in Rails
Problems Installing Ruby on Mountain Lion - Ruby 1.9.3 Wont' Compile