Enhances the Rails date/time parsing behavior to utilize the format strings already defined in your application (or the Rails defaults). Text input fields can now accept any date format, including %m/%d%/Y, without littering your models or controllers with conversion code in your application.
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



The smart_time gem allows your date/time ActiveRecord attributes to be parsed using the formats already defined by you or Rails in Time::DATE_FORMATS and Date::DATE_FORMATS. This allows you to capture date/time from text input fields using format strings that are already used by Rails to convert Dates and Times to Strings. If a :format option is passed smart_time will force the use of the supplied format, otherwise it will attempt to parse using the formats defined in Time::DATE_FORMATS (for Times) and Date::DATE_FORMATS (for Dates). If none of the formats match it will fail back to using the default Rails/Ruby parsing technique. Requires Rails 3.

Getting Started

  1. Add smart_time to your Gemfile:

    gem 'smart_time'
  2. Install:

    bundle install
  3. Use the smart_time macro in your models

    class Event < ActiveRecord::Base
      # Calling smart_time with no parameters activates smart_time for all columns that have a
      # klass of Date or Time (aka db types :date, :time, :datetime, and :timestamp).
      # Or you can name just the attributes you want to activate
      # smart_time :start_at, :end_at
      # You can also explicitly pass a format string
      # smart_time :start_at, :format => "%m/%d/%Y"
  4. Enjoy! If something breaks, complain to the author or (preferably) fork it, fix it, make sure all tests still pass and create a pull request.


  • Implement validation so that format errors can be properly displayed back to the view

Contributing to smart_time

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.


Copyright © 2011 Matthew Daubert. See LICENSE.txt for further details.