Aniket Kudale
Aniket Kudale

Reputation: 1

Rails not Date.strptime not getting accurate op

07/28/2017 11:56 PM This is my date and below is my current code:

Date.strptime("07/28/2017 11:56 PM", '%m/%d/%Y %H:%M %p').to_time

i'm getting o/p => 2017-07-28 00:00:00 +0530 but i want hours.What exactly should i do ?

Upvotes: 0

Views: 26

Answers (1)

Simple Lime
Simple Lime

Reputation: 11035

The problem is that you're converting it into a Date object which doesn't have any time component and then converting it back into a Time object. Instead, you can use either

Time.strptime("07/28/2017 11:56 PM", '%m/%d/%Y %H:%M %p')
# => 2017-07-28 23:56:00 -0700

or

DateTime.strptime("07/28/2017 11:56 PM", '%m/%d/%Y %H:%M %p')
# => Fri, 28 Jul 2017 23:56:00 +0000

and you call call to_time on the DateTime if you'd like

DateTime.strptime("07/28/2017 11:56 PM", '%m/%d/%Y %H:%M %p').to_time
# => 2017-07-28 23:56:00 +0000

Upvotes: 1

Related Questions