Date objects represent a single moment in time stored as a representation of the number of milliseconds since midnight on January 1st, 1970 UTC.
Note: This is different from a UNIX timestamp, which is the number of seconds since the Epoch (Midnight UTC on January 1st, 1970). Even though the representation of a
Date is in UTC, the methods to fetch a date or its components work in the host system’s local time zone (which may differ from UTC).
const myDate = new Date(dateValue);
dateValue can be any of the following:
- It can be a string representation of a date that is valid and IETF-compliant.
- It can be another instance of the
- It can be an integer that represents the date measured in milliseconds since January 1st, 1970 UTC.
The defined range for a
Date value is between April 20, 271821 BCE to September 13, 275760 CE. Individual datetime elements can be defined in the
Date() constructor with the following syntax:
const d = new Date(year, month, day, hour, minute, second, millisecond);
Date Format Types
|ISO Date (The International Standard)||
With regard to ISO dates:
- It is the only format that is strictly enforced while the others may vary in functionality depending on the browser.
- “YYYY-MM-DD” or “YYYY/MM/DD” is the preferred format.
- In the output, the
Tseparates the the date from the time while the
Zrepresents the UTC timezone.
- Existing dates can be converted to ISO with the
With regard to short dates:
- It is best practice to use leading zeros when referring to single-digit calendar days.
- If using “YYYY-MM-DD” returns
NaNthe alternative format “YYYY/MM/DD” should be tested (and vice versa).
With regard to long dates:
- The day and month can be in any order.
- The month can either be abbreviated (“Mar”) or written in full (“March”).
- Names are case-sensitive and commas can be ignored. (e.g.
new Date("MONTH DD YYYY"))
When used as a function,
Date() returns the current date and time. When used as a constructor,
Date() returns a new date object.
const now = Date();console.log(now);const then = new Date();console.log(then);
The output from the snippet above would look similar to this:
Fri Apr 22 2022 17:59:19 GMT+0000 (Coordinated Universal Time)2022-04-22T17:59:19.244Z
The example below defines a new
d. Many instance methods are used, which can be found at the bottom of this entry.
- Called from an instance of the Date class, will return the date of the month. All return values will be integers between 1 and 31.
- Called from an instance of the Date class, will return the day of the week.
- Called from an instance of the Date class, will return the hour according to the local time.
- Called from an instance of the Date class, will return the minutes according to the local time.
- Called from an instance of the Date class, will return the month of the year.
- Called from an instance of the Date class, will return the seconds according to the local time.
- Returns the current time in milliseconds. The milliseconds count begins at 1970/01/01 at 00:00:00 UTC.
- Returns a string representation of a date in an ISO 8601-compliant format.
- Returns a modified string from a given Date object, usually for events. It is translated to a specific language format according to an event locale and other options.
- Converts a Date object to a string.
- Returns a number value representing the number of milliseconds between the specified date and January 1, 1970, 00:00:00, Universal Time Coordinated. Will always be called as Date.UTC() rather than called on an instance of date such as myDate.UTC().
- Returns the difference in milliseconds between the specified date and January 1, 1970 00:00:00 UTC.