Having trouble using two datetimes variables in a conditional statement.
Here is an example output of variable one. Lets call it ontime_datetime:
Fri, 10 Feb 2017 16:30:00 +0000
Here is an example output of variable two. Lets call it delivery_timestamp:
Fri, 10 Feb 2017 13:15:00 -0600
I need to compare these two variables and find out which value is greater by date and time:
if delivery_timestamp <= ontime_datetime
f.update_attributes(on_time: true)
puts "#{f.tracking_number} ONTIME"
elsif delivery_timestamp > ontime_datetime
f.update_attributes(status_refund: true)
puts "#{f.tracking_number} NOT ONTIME"
else
f.update_attributes(other_result: true)
puts "#{f.tracking_number} OTHER"
end
When I run my script the second conditional is met, but the value of delivery_timestamp is before ontime_datetime, so the first conditional should be met. I think it has something to do with the timezone value? (which I don't need but not sure how to remove)
Either that, or I need to format the variable better for this type of conditional.
Added later by DK for more info ---- Here is how I am getting ontime_datetime:
json_response = JSON.parse(response)
transit_time = json_response['TrackPackagesResponse']['packageList'][0]['standardTransitTimeWindow']['displayStdTransitTimeEnd']
transit = json_response['TrackPackagesResponse']['packageList'][0]['standardTransitDate']['displayStdTransitDate']
transit_date = Date.parse Date.strptime(transit, '%m/%d/%Y').strftime("%Y-%m-%d")
transit_datetime = "#{transit_date} #{transit_time}"
ontime_datetime = DateTime.strptime(transit_datetime, "%Y-%m-%d %I:%M %p")
...and here is how I am getting delivery_timestamp:
delivery_timestamp = info_track[:delivery_timestamp]
The output value is the same format between ontime_datetime and delivery_timestamp, but the conditional evaluation isn't working properly. Any help (via code demonstration) would be great!
Please advise - DK