0

when I build a date form this ISO String "2016-02-01T16:00:00Z" I got Mon Feb 01 2016 17:00:00 GMT+0100

It seams that js is adding a hour for some reasons.

I think its a Timezone thing... but how can I fix this?

just do

var date = new Date('2016-02-01T16:00:00Z');
alert(date);

3 Answers3

2

According to specifications, the ISO date string is parsed as UTC+0000, which is indicated by the Z char at the end.

Z is the zone designator for the zero UTC offset

When you indicate a date time string for the Date() constructor, it's parsed in UTC.
The method Date.prototype.toString() is formatting the date according to your timezone, which may differ from UTC. Because of that you get this offset.

It's possible to indicate a custom timezone at the end of an ISO string with ±hh:mm:

var d = new Date('2016-02-01T16:00:00+01:00');
d.toString() // will print "Feb 01 2016 16:00:00 GMT+0100", if you're in GMT+01:00
Dmitri Pavlutin
  • 18,122
  • 8
  • 37
  • 41
  • 1
    +1, however, parsing strings with the Date constructor is unreliable and not recommended, far better to manually parse strings (a library can help but isn't necessary in most cases). – RobG Feb 27 '16 at 01:30
1

Javascript takes your datetime string, parses it in the timezone indicated (UTC), but then displays it in your current timezone.

When I run your code snippet, I get GMT-05:00 (EST).

So it's not adding an hour. It's just outputting the date in your local timezone.

jszobody
  • 28,495
  • 6
  • 61
  • 72
  • That's correct, it's the same time printed in different ways. This can be seen by printing date.toString() and date.toUTCString() – Sandman Feb 26 '16 at 19:49
0

You need take into consideration the TimeZoneOffset Date.getTimezoneOffset() to show same date in different time zones. For example get offset in minutes convert to hours and add it to you time, or write function to convert date with depend time zone offset Like here

Community
  • 1
  • 1
yavor.makc
  • 87
  • 1
  • 2