简体   繁体   中英

Convert string to datetime ruby on rails

I know this is basic but I've been struggling for a few hours now and I can't seem to apply one of the many ways there are to convert a string to datetime so I can save it in the database in this format 2018-03-16 00:12:17.555372 . Thanks ahead

This is the string output in the console.

params[:event][:start_date]
"03/28/2018 1:46 AM"

[EDIT] Following some leads I've come up with smething really dirty maybe someone can help refactor I'm supressing AM or PM because I don't know how to parse that I know it's awfull any help is appreciated!

  if !params[:event][:start_date].empty?
    start_date = params[:event][:start_date]
    start_date = start_date.gsub(/[AMP]/, '').squish 
    a = start_date.split('/')
    tmp = a[0]
    a[0] = a[1]
    a[1] = tmp   
    a = a.split(',').join('/')
    start_date = Time.parse(a)
  end

 if !params[:event][:end_date].empty?
   end_date = params[:event][:end_date]
   end_date = end_date.gsub(/[AMP]/, '').squish 
   a = end_date.split('/')
   tmp = a[0]
   a[0] = a[1]
   a[1] = tmp   
   a = a.split(',').join('/')
   end_date = Time.parse(a)
 end

You can use DateTime to parse the date from a specific format.

if the format you are looking to parse is "03/28/2018 1:46 AM" then you can do this.

date = DateTime.strptime('03/28/2018 1:46 AM', '%m/%d/%Y %I:%M %p')

# date to ISO 8601

puts date.to_time
# output: 2018-03-28 07:16:00 +0530

puts date.strftime("%m/%d/%Y")
# output: 03/28/2018

Date formats:

Date (Year, Month, Day):

%Y - Year with century (can be negative, 4 digits at least)
        -0001, 0000, 1995, 2009, 14292, etc.
%m - Month of the year, zero-padded (01..12)
        %_m  blank-padded ( 1..12)
        %-m  no-padded (1..12)
%d - Day of the month, zero-padded (01..31)
        %-d  no-padded (1..31)

Time (Hour, Minute, Second, Subsecond):

%H - Hour of the day, 24-hour clock, zero-padded (00..23)
%k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
%I - Hour of the day, 12-hour clock, zero-padded (01..12)
%l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
%P - Meridian indicator, lowercase (``am'' or ``pm'')
%p - Meridian indicator, uppercase (``AM'' or ``PM'')

%M - Minute of the hour (00..59)

You can refer to all formats here .

You can parse it like so in ruby:

Parses the given representation of date and time, and creates a DateTime object. This method does not function as a validator.

DateTime.parse('2001-02-03T04:05:06+07:00')
                          #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
DateTime.parse('20010203T040506+0700')
                          #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
DateTime.parse('3rd Feb 2001 04:05:06 PM')
                          #=> #<DateTime: 2001-02-03T16:05:06+00:00 ...>

Not entirely sure if the string you supplied can be parsed, here is the link to the ruby docs on datetimes Docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM