My datetimes are stored in the DB as UTC. When they are sent to the client I want to convert them to local time.
Javascript receives a given datetime as a string that looks like this:
2016-09-29T19:13:40
If I run new Date('2016-09-29T19:13:40')
on the string I get this output:
Thu Sep 29 2016 19:13:40 GMT-0400 (Eastern Daylight Time)
While the -0400 offset is present, it's not changing the time. Since UTC offsets aren't displayed to the user, it just looks like the timestamp is off by 4 hours.
Conversely if I do this:
new Date('2016-09-29T19:13:40').toISOString();
The output is:
2016-09-29T23:13:40.000Z
As you can see it actually adjusts the hours by 4 and eliminates the offset.
How come when I feed javascript a UTC datetime to convert to local all it does is add an offset, but when I feed it what it thinks is a local datetime it and ask it to convert to UTC it actually adjusts the time?
How can I get it to adjust a UTC datetime to local the way it adjusts local to UTC?
EDIT
Here's a fiddle: https://jsfiddle.net/qjmfLu67/1/
I'm getting different behavior between IE11 and Chrome. IE11 doesn't convert UTC to local. Chrome does convert UTC to local and if it recognizes a date as UTC, when you run toISOString
on it, it doesn't adjust anything.