Cannon Moyer
Cannon Moyer

Reputation: 3174

Invalid DateTime - Ruby

I'm trying to format a datetime string that I receive as a parameter. However, I'm getting an invalid date error.

The parameter params[:job][:start] for example has the value of 02/22/2018 5:20 PM.

I am getting an invalid date error from the code below. I am trying to format the date parameter to this format YYYY-MM-DD HH:MM:00 +0000

Please notice that the parameter being sent is in 12 hour time and the format that I am trying to format that parameter to is in 24 hour time.

DateTime.strptime(params[:job][:start],"%Y/%m/%d %H:%M:00 +0000")

Upvotes: 0

Views: 437

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

You should parse the string into the DateTime instance in the first place:

DateTime.strptime('02/22/2018 5:20 PM', '%m/%d/%Y %l:%M %p')

Then you might spit it out in any format you want.

More info on formatters.

Upvotes: 2

Related Questions