Home > Software engineering >  Why JavaScript displays date and time in local time zone?
Why JavaScript displays date and time in local time zone?

Time:09-26

I was going through a citation from the MDN :

Note: It's important to keep in mind that while the time value at the heart of a Date object is UTC, the basic methods to fetch the date and time or its components all work in the local (i.e. host system) time zone and offset.

If JavaScript Date Objects by default stores the time in UTC then how it displays time in local time zone. Does it internally convert it into local time zone or what?

CodePudding user response:

Append 'UTC' to the string before converting it to a date in javascript:

var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"

CodePudding user response:

While it's a very useful resource, MDN is not normative. It's a public wiki that anyone can modify. The normative documentation is ECMA-262 (currently ECMA-262 ed 12 or ECMAScript 2021), a new edition of which is published every year in June. The current draft will become ECMASCript 2022.

How Date objects work is fully detailed in ECMA-262, MDN is a summary in more understandable terms. As with all summaries, it doesn't contain all the information in the standard and some of the explanations aren't exact.

ECMASCript Dates contain a single data value, a time value that is a number of milliseconds since 1970-01-01T00:00:00Z. That's why Date objects are considered to be inherently UTC. The value could be stored as say an ISO 8601 formatted string or set of date and time values, but a time value is a good common denominator and calculating it would likely be the first step in most date methods anyway. It's a common concept used in many programming languages and environments.

How the time value is calculated depends on how many parameters are passed to the Date constructor when called and, for the first parameter, its type as described in the section on the Date constructor.

How the local timezone offset is calculated is implementation dependent, as described in the LocalTZA ( t, isUTC ) section:

LocalTZA( t, isUTC ) is an implementation-defined algorithm that returns an integral Number representing the local time zone adjustment, or offset, in milliseconds.

It depends on access to the host system regional settings and time, plus historic timezone offset changes for the region (earlier versions of ECMASCript required that the current settings be treated as if they were always in force, which was not ideal).

So to answer the question:

If JavaScript Date Objects by default stores the time in UTC then how it displays time in local time zone. Does it internally convert it into local time zone or what?

The local timezone offset for the particular date and time represented by internal UTC offset (time value) is applied to the to determine local values for that particular instant. The algorithms are all in ECMA-262 as Make Time, Make Day, Make Date, etc.

  • Related