Date – JavaScript | MDN
A date is represented internally as a single number, the timestamp. When interacting with it, the timestamp needs to be interpreted as a structured date-and-time representation. There are always two ways to interpret a timestamp: as a local time or as a Coordinated Universal Time (UTC), the global standard time defined by the World Time Standard. The local timezone is not stored in the date object, but is determined by the host environment (user’s device).
Note: UTC should not be confused with the Greenwich Mean Time (GMT), because they are not always equal — this is explained in more detail in the linked Wikipedia page.
For example, the timestamp 0 represents a unique instant in history, but it can be interpreted in two ways:
- As a UTC time, it is midnight at the beginning of January 1, 1970, UTC,
- As a local time in New York (UTC-5), it is 19:00:00 on December 31, 1969.
The getTimezoneOffset()
method returns the difference between UTC and the local time in minutes. Note that the timezone offset does not only depend on the current timezone, but also on the time represented by the Date
object, because of daylight saving time and historical changes. In essence, the timezone offset is the offset from UTC time, at the time represented by the Date
object and at the location of the host environment.
There are two groups of Date
methods: one group gets and sets various date components by interpreting the timestamp as a local time, while the other uses UTC.
The Date()
constructor can be called with two or more arguments, in which case they are interpreted as the year, month, day, hour, minute, second, and millisecond, respectively, in local time. Date.UTC()
works similarly, but it interprets the components as UTC time and also accepts a single argument representing the year.
Note: Some methods, including the Date()
constructor, Date.UTC()
, and the deprecated getYear()
/setYear()
methods, interpret a two-digit year as a year in the 1900s. For example, new Date(99, 5, 24)
is interpreted as June 24, 1999, not June 24, 99. See Interpretation of two-digit years for more information.
When a segment overflows or underflows its expected range, it usually “carries over to” or “borrows from” the higher segment. For example, if the month is set to 12 (months are zero-based, so December is 11), it become the January of the next year. If the day of month is set to 0, it becomes the last day of the previous month. This also applies to dates specified with the date time string format.