2

I am defining a new Date object: const date = new Date(2020, 09, 11); The documentation says that the default time for the constructor is: hours : 0, minutes : 0, seconds : 0... etc. However, the result I get is:

const date = new Date(2020, 09, 11);
console.log(date);
2020-10-11T07:00:00.000Z

Why is the hour 7?

When I put the following into the console:

const date2 = new Date(2020, 09, 11, -7)
console.log(date2)
2020-10-11T00:00:00.000Z

I'm at a bit of a loss here.

  • 2
    That Z at the end stands for Zulu time = GMT. Since your difference is -7 I presume you are located at US Pacific Time. It's telling you that **YOUR** midnight is at 7am in the UK. – slebetman Sep 28 '21 at 22:59
  • 1
    If you are running the code in your browser then your timezone settings is determined by a combination of your brower's locale and your OS time settings. If you are running the code in node.js then your timezone settings is determined by your OS time settings. By default a lot of OSes track time in GMT but format the time to the local time ONLY for display purposes. Node is showing you the OS internal time. The reason for this is to simplify global time synchronization with other users on the internet – slebetman Sep 28 '21 at 23:00
  • 1
    Also note; you'll get an error in nodeJS for `09` due to strict mode. – Andy Sep 28 '21 at 23:03
  • 1
    @Andy's comment refers to the fact that you are trying to pass an octal number 09 (any number that starts with 0 is an octal number, for example the value of `010` is `eight` not `ten`). The digit `9` is an illegal digit in octal numbers since octal numbers can only contain the digits 0,1,2,3,4,5,6 and 7. – slebetman Sep 28 '21 at 23:05
  • Does this answer your question? [Why does Date.parse give incorrect results?](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – traktor Sep 28 '21 at 23:06
  • Sorry, I was wrong. Javascript keeps track of time in UTC. It's not really based on OS time settings (though you can definitely screw up Javascript's understanding of time by misconfiguring your OS). My previous comment was influenced by my experience with other programming languages. – slebetman Sep 28 '21 at 23:15
  • @RobG oops & thx! - I've deleted reference to the previous item in the article - your link correctly identifies use of local time for the parameter values. – traktor Sep 29 '21 at 04:46
  • @slebetman—"locale" seems to have morphed into a synonym for language (which I strongly disagree with but there you go), and language has nothing to do with timezone offset, which comes from host system settings. Currently it's popular to use a location (most likely an IANA representative location) as a reference for timezone offset calculations, which may or may not reflect the system's actual location (e.g. it's common for servers to be set to UTC/GMT even though they're nowhere near 0° longitude). :-) – RobG Sep 29 '21 at 08:01
  • @RobG Locale is more than just language. Language is usually called "language". Locale covers language + date time format + timezone + currency format etc. It is **what-does-your-computer-need-to-be-configured-based-on-your-location-and-culture** – slebetman Sep 29 '21 at 08:35
  • @RobG Also, location is not usually called "locale" in IT. The word we use for location is usually "country" or "location" or "address" or "coordinates" etc. "Locale" is location + language + timezone + date time format + currency format etc. – slebetman Sep 29 '21 at 08:38
  • @slebetman—I don't think IT in general is so confused as to the meaning of "[*locale*](https://www.merriam-webster.com/dictionary/locale)". On my PC, there are settings for "language and region" and other settings for "date and time". Nothing to do with location or culture, just personal settings for how I want things on my PC. :-) – RobG Sep 29 '21 at 08:59

2 Answers2

4

That Z at the end stands for Zulu time = GMT. Since your difference is -7 I presume you are located at US Pacific Time. It's telling you that YOUR midnight is at 7am in the UK.

Javascript internally keeps track of time in UTC. The reason for this is to simplify global time synchronization with other users on the internet.

To display your local time do:

console.log(date.toLocaleString());

See also this answer to a related question: How to initialize a JavaScript Date to a particular time zone

slebetman
  • 109,858
  • 19
  • 140
  • 171
0

The displayed time is correct, but in a different time zone. The standard timezone in most programming languages is called UTC (Universal Time Coordinated), GMT (Greenwich Mean Time) or Zulu Time. You can see it by the "Z" at the end of your time string. According to the local setup on your computer you can use date.toLocaleString() --> But even here problems can arise, when your users did set their computer/browser time to a different timezone. You can let your users set their timezones themselves and give these parameters to toLocaleString() according to https://www.w3schools.com/jsref/jsref_tolocalestring.asp

Tom
  • 89
  • 6