cleverpaul
cleverpaul

Reputation: 945

Why does Javascript show me the wrong date?

Consider the following date object which is created in JavaScript.

var date = new Date("2017-09-07T16:46:06.000Z");

This date object should be equivalent to Sep 7 2017 4:46:06 PM

However, in the browser console, when I type the following:

console.log(date);

The following is returned:

Fri Sep 08 2017 02:46:06 GMT+1000 (E. Australia Standard Time)

The time is wrong. (It actually is today's date, but the time is completely wrong).

Key points of confusion:

Upvotes: 0

Views: 74

Answers (2)

Zuraiz Zafar
Zuraiz Zafar

Reputation: 11

var date = new Date("2017-09-07T16:46:06.000Z");

So it looks like the Z at the end of your date string is meant to represent UTC or Zulu time

var date = new Date("2017-09-07T16:46:06.000");

should be the correct solution

Upvotes: 1

Sam R
Sam R

Reputation: 702

When you created the date, you did specify a timezone. That Z at the end means Zulu or Greenwich Mean Time. Your computer is 10 hours off from GMT, so it adjusts to your local timezone for display.

If you want the date to be in your local time zone, remove the Z

Upvotes: 6

Related Questions