JavaScript/Dates

In JavaScript, a Date is an object. Hence it must be explicitly created with the  operator.

Date contains a value that represents the number of milliseconds that have elapsed since January 1, 1970 UTC. It's worth mentioning what it does not contain: There is no information about a timezone within the object. Nevertheless, you can convert it to an appropriate string of any arbitrary time zone. Other methods can select parts like the month or the day of the week, or you can use the number for any computation, and more.

Constructor
The default constructor creates the Date object as of the current point in time of your computer.

You can pass parameters to the constructor to generate a certain Date object.

Methods
Some often used methods of Date are:

Static methods
 * Date.now: Returns the number of milliseconds since January 1, 1970, 00:00:00 UTC calibrated (plus/minus some hours) to the timezone of your computer.
 * Date.UTC: Returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
 * Date.parse(text): Parsing of strings with Date.parse is strongly discouraged due to browser differences and inconsistencies.

Instance methods
 * toISOString: Returns a string in ISO 8601 format.
 * getFullYear: Returns the full 4-digit year.
 * getMonth: Returns the current month. [0 - 11]
 * getDate: Returns the day of the month. [1 - 31]
 * getDay: Returns the day of the week. [0 - 6]. Sunday is 0, with the other days of the week taking the next value.
 * getHours: Returns hours [0 - 23] based on a 24-hour clock.
 * getMinutes: Returns minutes. [0 - 59]
 * getSeconds: Returns seconds. [0 - 59]
 * getTime: Returns the time in milliseconds since January 1, 1970.
 * valueOf: Returns the time in milliseconds since January 1, 1970. Equivalent to getTime.
 * getTimezoneOffset: Returns the difference in minutes between UTC and local time.
 * setFullYear(year): Stores the full 4-digit year within the Date object.
 * setMonth(month, day): Sets the month, and optionally the day within the month. '0' is January, ...

'As Integer'
The Date can be returned as an integer by the method  or by prefixing the constructor with a   sign, e.g., to use it for "seeding" a PRNG (Pseudo Random Number Generator) or to perform calculations.

Timezones
The Date object purely contains a whole number (Integer). It does not know anything about timezones. As long as you don't specify timezones in any form, you work in your local timezone.

But sometimes it's necessary to consider timezones.

New API: Temporal
The object Date has some inconsistencies and shortcomings, especially: weak timezone support, no support for non-Gregorian calendars, missing calendar functions, and more. Possibly they will be fixed in a new Temporal API in one of the subsequent JavaScript versions.

Exercises

 * ... are available on another page (click here).