Skip to content

JavaScript Date API

JavaScript Date methods for reading and formatting dates and times.

1 class · 7 methods

Date

7 methods

Represents a single moment in time (milliseconds since Unix epoch).

date.getDate()

Return the day of the month (1..31) in local time.

Returns

number

Example

javascript
new Date('2024-06-15').getDate()  // 15
date.getMonth()

Return the month (0..11, where 0 = January) in local time.

Returns

number

Example

javascript
new Date('2024-06-15').getMonth()  // 5 (June)
date.getFullYear()

Return the 4-digit year in local time.

Returns

number

Example

javascript
new Date('2024-06-15').getFullYear()  // 2024
date.toISOString()

Return the date in ISO 8601 format (UTC): YYYY-MM-DDTHH:mm:ss.sssZ.

Returns

string

Example

javascript
new Date('2024-06-15T12:00:00Z').toISOString()
// '2024-06-15T12:00:00.000Z'
date.toLocaleDateString(locale?, options?)

Return a string with a language-sensitive representation of the date.

Parameters

NameTypeDescription
localestring | string[]BCP 47 locale tag (e.g. 'en-US').
optionsobjectFormatting options.

Returns

string

Example

javascript
new Date(2024, 5, 15).toLocaleDateString('en-US')
// '6/15/2024'
new Date(2024, 5, 15).toLocaleDateString('zh-CN')
// '2024/6/15'
Date.now()

Static method. Return the number of milliseconds elapsed since Unix epoch.

Returns

number

Example

javascript
Date.now()  // e.g. 1718452800000
const start = Date.now();
// ... do work ...
console.log(Date.now() - start, 'ms');
Date.parse(dateString)

Static method. Parse a date string and return milliseconds since epoch, or NaN.

Parameters

NameTypeDescription
dateStringstringDate string (ISO 8601 recommended).

Returns

number

Example

javascript
Date.parse('2024-06-15T00:00:00Z')  // 1718409600000
Date.parse('not a date')            // NaN