Date API is cumbersome to use. Hence, it’s best to rely on a library for anything related to dates. Popular libraries include:
Consult the blog post “Why you shouldn’t use Moment.js…” for the pros and cons of these libraries.
UTC (Coordinated Universal Time) and GMT (Greenwich Mean Time) have the same current time, but they are different things:
UTC: is the time standard that all times zones are based on. They are specified relative to it. That is, no country or territory has UTC as its local time zone.
GMT: is a time zone used in some European and African countries.
Source: “The Difference Between GMT and UTC” at TimeAndDate.com
Date time formats describe:
The following is an example of a date time string returned by
Date time formats have the following structures:
Date formats: Y=year; M=month; D=day
YYYY-MM-DD YYYY-MM YYYY
Time formats: T=separator (the string
'T'); H=hour; m=minute; s=second and millisecond; Z=time zone is UTC (the string
THH:mm:ss.sss THH:mm:ss.sssZ THH:mm:ss THH:mm:ssZ THH:mm THH:mmZ
Date time formats: are date formats followed by time formats.
Z – time zones relative to UTC:
A time value represents a date via the number of milliseconds since 1 January 1970 00:00:00 UTC.
Time values can be used to create Dates:
Coercing a Date to a number returns its time value:
Ordering operators coerce their operands to numbers. Therefore, you can use these operators to compare Dates:
The following methods create time values:
Returns the current time as a time value.
Date.parse(dateTimeString): number (local time zone)
dateTimeString and returns the corresponding time value.
Date.UTC(year, month, date?, hours?, minutes?, seconds?, milliseconds?): number
Returns the time value for the specified UTC date time.
Returns the time value corresponding to the Date.
this to the date encoded by
new Date(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, milliseconds?: number) (local time zone)
new Date(dateTimeStr: string) (UTC)
new Date(timeValue: number)
new Date() (same as
Dates have getters and setters for time units. For example:
These getters and setters conform to the following patterns:
These are the time units that are supported:
Month: month (0–11). Pitfall: 0 is January, etc.
Date: day of the month (1–31)
Day(getter only): day of the week (0–6); 0 is Sunday
Hours: hour (0–23)
Minutes: minutes (0–59)
Seconds: seconds (0–59)
Milliseconds: milliseconds (0–999)
There is one more getter that doesn’t conform to the previously mentioned patterns:
Returns the time difference between local time and UTC in minutes. For example, for CET, it returns
Date.prototype.toTimeString() (local time zone)
Date.prototype.toLocaleTimeString() (see ECMAScript internationalization API)
Date.prototype.toDateString() (local time zone)
Date.prototype.toLocaleDateString() (ECMAScript internationalization API)
Date.prototype.toString() (local time zone)
Date.prototype.toLocaleString() (see ECMAScript internationalization API)
Exercise: Creating a date string
You can’t specify a local time zone. That can lead to location-specific bugs if you are not careful. For example, the following interaction leads to different results, depending on where it is executed. We execute it in CET, which is why the ISO string (in UTC) has a different day of the month (26 vs. 27).
January is 0, February is 1, etc.:
y with 0 ≤
y ≤ 99, 1900 is added: