code tunes

Web applications, software engineering, Ruby on Rails, Cake PHP, JavaScript, etc.

Archive for the ‘date formatting’ tag

Formatting dates for SQL queries in Rails

without comments

Here’s a simple and clean way to format date for use in SQL query in Ruby On Rails.

User.find :all, :conditions => ['created_at > ?', 7.days.ago.to_formatted_s(:db)]

You can use to_formatted_s(format) method with different data types - Date, Time, DateTime, Range, Array and BigDecimal. There are also more formats available apart from :db. Find out more in Rails API.

Written by Michał Szajbe

July 1st, 2008 at 5:40 pm