0

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.

Legion
  • 3,922
  • 8
  • 51
  • 95
  • Can't reproduce, I'm `GMT+0200` and it changes the date just fine for me, ending up with `21:13:40` etc. – adeneo Sep 29 '16 at 20:09
  • Running it on my console I get this: `new Date('2016-09-29T19:13:40') Thu Sep 29 2016 16:13:40 GMT-0300 (E. South America Standard Time)` – Fabricio Koch Sep 29 '16 at 20:12
  • Needs more context -- are you trying to run this in your browser? Also your "converse" example is actually just making the same assumptions as your initial statement. – Michael Nakayama Sep 29 '16 at 20:14
  • I also can't reproduce and is working fine for me. I am at PST (-0700), and here's what my Chrome console shows: new Date('2016-09-29T19:13:40') >> Thu Sep 29 2016 12:13:40 GMT-0700 (PDT) – gil.neo Sep 29 '16 at 20:21
  • Possible duplicate of [Convert UTC date time to local date time using JavaScript](http://stackoverflow.com/questions/6525538/convert-utc-date-time-to-local-date-time-using-javascript) – Heretic Monkey Sep 29 '16 at 20:21
  • @gil.neo I added a fiddle. It seems to behave differently between IE11 and Chrome. – Legion Sep 29 '16 at 20:22
  • @MichaelNakayama Yes, it's in the browser. Originally IE11, but I'm testing in Chrome and getting different behavior. – Legion Sep 29 '16 at 20:24
  • you're right, I see the issue in IE11 ... makes me hate IE even more :-P – gil.neo Sep 29 '16 at 20:44
  • @Legion my solution is to not use IE. My other solution is to use moment js like suggested below. – Michael Nakayama Sep 30 '16 at 16:34
  • @MichaelNakayama I'd love to drop IE support. Unfortunately this is an intranet application and IE support is required. I'm currently testing moment js to see if I can use it to resolve the issue. – Legion Sep 30 '16 at 16:52

3 Answers3

0

Working with date is not easy, especially if you do TZ conversions. I'd suggest to use momentjs (http://momentjs.com/docs/) for it, like I do.

// create a utc-zone moment
var x = moment.utc('2016-09-29T19:13:40')

x.format()
// output is "2016-09-29T19:13:40Z"

// adjust offset for the initial moment x by the local offset we get from moment created against local tz
x.utcOffset(moment().utcOffset());

x.format()
// output is "2016-09-29T22:13:40+03:00"
Vladimir M
  • 4,403
  • 1
  • 19
  • 24
0

Looks like for IE11, you'll have to pass the date string in full javascript format for it to work properly.

new Date("Thu Sep 29 2015 19:13 GMT")
gil.neo
  • 173
  • 7
0

new Date('2016-09-29T19:13:40')

This is missing milliseconds and "Z" which are needed for a correct conversion from UTC to Local.

new Date('2016-09-29T19:13:40.000Z') should give you the correct time relative to your machine.