Formatting a date string for database use in Ruby on Rails

Posted on 20 April 2009

Sometimes you need to format a date / time correctly for database querying and sneaked into Rails 2.1 was a really helpful method for doing just that.

Time.now.to_s(:db)

So the above would change Mon Apr 20 16:15:00 +0100 2009 to "2009-04-20 16:15:00".

Got something to say?