Handling dates when using Rails 4, bootstrap and bootstrap3-datetimepicker-rails gem

The bootstrap3-datetimepicker-rails is a very nice plugin that adds a nice date and time picker to your bootstrap based rails application. I started using it the other day and I ran into huge problems. I will show you what I did and explain why I did it rather than explain the problems before hand.

The first thing I had to do was to convert the date to something the model could save. In rails 4 due to strong parameters you have to use a < controller >_params function to filter out unwanted fields. This is where I first convert my date so the model doesn’t complain. lets assume my controller is called events and I am taking a start date from the view

class EventsController < ApplicationController
.... your code
  def event_params
    valid = params.require(:event).permit(:id,:title,:description,:start_date) 

    date_format = "%m/%d/%Y %I:%M %p"
    offset = DateTime.now.strftime("%z")
    valid[:start_date] = valid[:start_date] != "" ? DateTime.strptime(valid[:start_date], date_format).change(:offset => offset).to_s : valid[:start_date]
    return valid
  end

so first I collect my valid params in a variable named valid. next I check if the date is empty, otherwise I use the following to convert the date to something the model will enjoy. Also notice that I am setting the zone offset too. This is important if you plan to implement reminders too:

   offset = DateTime.now.strftime("%z")
   DateTime.strptime(valid[:start_date], date_format).change(:offset => offset).to_s

as you can see I pass date format which is how the date picker is formatting it on the view
once the conversion is done I return valid (p.s. I think in ruby you don’t have to explicitly return the valid since it is the last statement but I am too lazy to check)

So that takes care of on problem: Saving the date
For my second problem I need to display the data correctly on the view. If I skip this step then users will get something like this: 2014-11-30 05:30:00 -0000
which is not nice and it breaks the date picker. So to do this I created the following file in the helpers folder and added a helper function

app/helpers/events_helper.rb

module EventsHelper
  def date_for_display(date)
    fsdate = (date == nil)? date :  I18n.l( DateTime.parse(date.localtime.to_s), :format => :short)
  end
end

I use the I18n.l gem to convert that date to something like this 11/30/2014 10:30 AM

Now, on the controller I just include the helper like so:

  class EventsController < ApplicationController
    helper :events
....

Note: you can use the helper on other controllers too. Just use the same ” helper :events ”
and on the view I can now just do

date_for_display( @event.start_date)

Now the user can see the time in the format that the date picker is adding it to the input field. Obviously this only works for a single timezone. I plan on adding time zones later but hopefully this will point you in the right direction

P.S.: I believe to implement timezones correctly you first need to save the user timezone and then set the time zone in the controller
you might refer to this post for help Dealing With Timezones Effectively in Rails

Leave a Reply

Your email address will not be published. Required fields are marked *