Search code examples
ruby-on-railsdatetimepickerjquerydatetimepicker

Convert jQuery datetimepicker date and time format into a Rails DateTime format


I would need to implement jQuery datetimepicker into my Ruby on Rails 5 application. There is good documentation for jqueryui Date picker, included two railscasts by Ryan Bates.
As datetimepicker is concerned, I suppose the only concern about rails is to produce a date and time format that rails can easily understand. Once this issue is solved, I think it is easy to add the plugin to the asset pipeline.

Rails DateTime format is "yyyy-mm-dd hh:mm:ss". For instance "2017-07-28 11:24:53".
So I wonder: is there any way to change the default datetimepicker format into a DateTime rails format ?

In the datetimepicker official webpage, the section dedicated to using another date parser/formatter uses as example the MomentJS library. However I could not find examples useful for Rails applications.

jQuery UI Datepicker has a dateFormat option which can be easily used to set the date format to the desired format: how can this be done with datetimepicker?


Solution

  • In your view :

    = form_for @event do |f|
      = f.text_field :event_start, class: "datepicker"
    

    In your controller

    def create
      # before the save
      @event.event_start = params[:event][:event_start].to_datetime
    end