I need this
require 'date'
DateTime.parse "Mon, Dec 27 6:30pm"
to return a DateTime for 6:30pm in the EDT timezone, but it returns one in UTC. How can I get a EST DateTime or convert the UTC one into an EDT DateTime with a 6:30pm value?
I need this
require 'date'
DateTime.parse "Mon, Dec 27 6:30pm"
to return a DateTime for 6:30pm in the EDT timezone, but it returns one in UTC. How can I get a EST DateTime or convert the UTC one into an EDT DateTime with a 6:30pm value?
In Rails, this worked nicely for me
It won't work in vanilla Ruby though.
Final answer ;-)
(or -4 for EDT)
OK I'm going to offer an answer to my own question
DateTime#change()
You can try using
change()
after parsing it to alter the timezone offset:You can also just use the hours:
But, be careful, you cannot use an integer:
If you need to determine the correct offset to use based on a time zone you can do something like this:
You can also use
change()
to manually set other parts of theDateTime
as well, like setting thehour
to noon:Be careful with that one because you can see that it's cleared the minutes as well.
Here's the docs for the
change()
method: http://api.rubyonrails.org/v5.1/classes/DateTime.html#method-i-change