Ruby String to Date Conversion Ruby String to Date Conversion ruby ruby

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 date2010-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 str2010-08-10puts Date.parse(Time.parse(str).utc.to_s)2010-08-09

I couldn't find simpler method to convert Time to Date.


Date.strptime(updated,"%a, %d %m %Y %H:%M:%S %Z")

Should be:

Date.strptime(updated, '%a, %d %b %Y %H:%M:%S %Z')


str = "Tue, 10 Aug 2010 01:20:19 -0400 (EDT)"str.to_date=> Tue, 10 Aug 2010