Tweak CalendarDateSelect plugin to work with European datetime format

Calendar date select in action

If you want to use calendar_date_select plug-in for date time picker, and you would like to use dd.mm.yyyy format read on!

I wrote this post after many wrong turns and many tutorials that just didn’t work. This informations applies for RAILS 2.3.2, and Calendar Date Select 1.15. I you are anything like me you want proof that my tutorial is working? OK, go hereBUT you should have trusted me in the first place :) After you installed the plug-in, unpack it so you can use it on shared hosting (I am using Heroku).

rake gems:unpack

In file:

/vendor/gems/calendar_date_select-1.15/lib/calendar_date_select/calendar_date_select.rb

change self.format to:

 def self.format
     @format ||= FORMATS[:finnish]
 end

Note: Alternative is to use

<% CalendarDateSelect.format=(:finnish )%>

in head tag of your layout page, but for some reason it’s not working on first page load on Heroku.

Almost there :) now in you need to add file date_formats.rb to folder

/config/initializers

with this content:

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!
(:default => "%d.%m.%Y")
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!
(:default => "%d.%m.%Y %H:%M")

Initializers get called by default, so you don’t have to do anything else with them. This is used for formating date in wanted format when using to_s. This works perfecly on my development machine that has dd.mm.yyyy as a system date format. If that’s case with you, then you probably didn’t need this tutorial at all – go surf some place else :)

However on Heroku your day and month will switch places everytime you save model to a DB. It turns out that Rails is using heuristic to determine date time
format. Idea is not bad, but format is highly ambigous. Some suggest that you should change default Date._parse method – I did not want to do that. Some say that you need to change ActiveRecord::ConnectionAdapters::Column:string_to_date method. The problem with that approact is that by the time you get into ActiveRecord::ConnectionAdapters::Column:string_to_date , the model date is already “switched” – I think this was not a fact when that tutorial was written. At the end and after much head-banging I decided to help the default Date._parse method to do the right thing.

I knew that if you have 06/23/2009 it will get interpreted as American format (mm/dd/yyyy) and that if you have 06-23-2009 it will get interpreted as European format (dd-mm-yyyy). Format that I wanted to use was dd.mm.yyyy – it was correctly interpreted on my machine but not on Heroku hosting. Let’s say that you have model :event and datetime filed :event_date.

The right time for “strike” is before you send parms[:event] in you controller. I didn’t want to write call to that for create or update so I used before_filter.

class EventsController < ApplicationController
before_filter :localize_params,  : only => [:update, :create ]

def localize_params
   params[:event][:event_date].gsub!('.','-')
end

Easy-peasy…. Now it should work as expected.

  • Kresimir Bojcic
    Great to hear from you. Btw. I am currently checking up VIM for my rails development... it's a bumpy ride, me being use to .NET Visual Studio but it's looking promising... So Aptana is out of mine experties :)
  • Chamba
    Thanks, finally I was able make my rails application accept the date format that I really wanted to use!

    However, at the config/initializers I changed the lines to the following:

    ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.
    merge!(default => '%d/%m/%Y')

    Otherwise I was getting an error in Aptana Studio.

    Cheers!
blog comments powered by Disqus