Search code examples
ruby-on-railsrubytwitter-bootstrapbootstrap-datepickerbootstrap-datetimepicker

How can I convert "11/23/2014 8:35 PM" to "2014-11-16 07:45:00" in Ruby/Rails?


I am using rails 4.1 and Ruby 2.1.

I am getting "11/23/2014 8:35 PM" passed to the server as part of a form using bootstrap-datetimepicker, and I would like to convert it like this (or something similar):

pu = "11/23/2014 8:35 PM".to_time #or something like this
@appointment = @car.appointments.build(pickuptime: pu)

This .build call is expecting the time to be formatted as 2014-11-16 07:45:00. How can I make this conversion? Thanks!


Solution

  • You can use DateTime.strptime and specify the format as the second parameter

    pu = DateTime.strptime("11/23/2014 8:35 PM", "%m/%d/%Y %l:%M %p")
    @appointment = @car.appointments.build(pickuptime: pu)