3

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
2
  • 1
    Where does params[:event][:start_date] come from? Is it input from a HTML form, a date picker maybe? If so, you can probably change it to pass an ISO 8601 compliant string instead. Commented Mar 16, 2018 at 7:26
  • it's from bootstrap datetime picker within the formno idea how to change the string to be iso 8601 compliant do you have a resource you can link here @stefan ? Commented Mar 16, 2018 at 9:35

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect works like a charm thanks for your help and explanation @Gautam Chibde
2

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

1 Comment

I'm trying and it's working kind of but I feel like it's wrong I'm chopin am or pm i edited the thread. Thanks @RickS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.