Parsing string date, time and datetime with Rails

I have been working with dates since I’ve started programming professionally. Initially you have probably googled: “how to convert date to string” or “ruby convert string to datetime”. Eventually, you will probably graduate to just searching “strftime cheatsheet” or “strptime cheatsheet”.

Recently I discovered that ActiveSupport has conversions for strings. This means that if your using Rails, you can actually do following.

Convert string to date:


Convert string to datetime:

"30/Nov/2009 16:29:30 +0100".to_datetime

Convert string to time:


Unlike strftime or strptime, these conversion methods are not as flexible. However, knowing these options could help you save a ton of time if the opportunity presents itself.