How do I create a Ruby date object from a string?

RubyDate

Ruby Problem Overview


How do I create a Ruby date object from the following string?

DD-MM-YYYY

Ruby Solutions


Solution 1 - Ruby

Date.parse('31-12-2010')

Alternatively Date#strptime(str, format).

Solution 2 - Ruby

Because in the USA they get the dates backwards, it's important not to simply use Date.parse() because you'll find 9/11/2001 can be 11 September 2001 in the USA and 9 November 2001 in the rest of the world. To be completely unambiguous use Date::strptime(your_date_string,"%d-%m-%Y") to correctly parse a date string of format dd-mm-yyyy.

Try this to be sure:

>irb
>> require 'date'
=> true
>> testdate = '11-09-2001'
=> "11-09-2001"
>> converted = Date::strptime(testdate, "%d-%m-%Y")
=> #<Date: 4918207/2,0,2299161>
>> converted.mday
=> 11
>> converted.month
=> 9
>> converted.year
=> 2001

For other strptime formats see http://pubs.opengroup.org/onlinepubs/009695399/functions/strptime.html

Also I always make sure I set my base timezone to :utc if my website is going to be handling any dates, and use Javascript on the client side to display local times.

Solution 3 - Ruby

You can use Time#parse.

Time.parse("20-08-2010")
# => Fri Aug 20 00:00:00 +0200 2010

However, because Ruby could parse the date as "MM-DD-YYYY", the best way is to go with DateTime#strptime where you can specify the input format.

Solution 4 - Ruby

If you have control over the format of the date in the string, then Date.parse works fine internationally with strings in YYYY-MM-DD (ISO 8601) format:

Date.parse('2019-11-20')

Solution 5 - Ruby

I find this approach simpler since it avoid having to specify the date format for the parser:

> date1 = Time.local(2012, 1, 20, 12, 0, 0).to_date

Solution 6 - Ruby

Like this You can get time Object from a string like this:

t = Time.parse "9:00 PM" 
 => 2013-12-24 21:00:00 +0530

t = Time.parse "12:00 AM"
 => 2013-12-24 00:00:00 +0530 

But Ruby parsing this as a Date!

So you can use the column as a string.

add_column :table_name, :from, :string, :limit => 8, :default => "00:00 AM", :null => false
add_column :table_name, :to, :string, :limit => 8, :default => "00:00 AM", :null => false 

And you can assign string object to the attribute,

r.from = "05:30 PM"
r.save

And parse the string for getting time object,

Time.zone.parse("02:00 PM")

Solution 7 - Ruby

Not necessary for this particular string format, but best string to time parsing utility I know is Chronic which is available as a gem and works for about 99.9% of usecases for human formatted dates/times.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbenView Question on Stackoverflow
Solution 1 - RubydecezeView Answer on Stackoverflow
Solution 2 - RubyDave SagView Answer on Stackoverflow
Solution 3 - RubySimone CarlettiView Answer on Stackoverflow
Solution 4 - RubyJon SchneiderView Answer on Stackoverflow
Solution 5 - RubyjpwView Answer on Stackoverflow
Solution 6 - RubyAbhiView Answer on Stackoverflow
Solution 7 - RubybglusmanView Answer on Stackoverflow