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".

About Paul

Paul works for Kyan web design agency in Surrey, UK as a Ruby on Rails developer.

Follow Paul on Twitter

Email: paulsturgess [at] gmail.com

Read more articles in the archive →

Comments...

  • Thanks, it was helpful.

    Waqas at 22 Jul 10 at 06:13

Got something to say?