Date Object Method Reference

The JavaScript Date object has a number of methods that help us to work with dates. The table below lists the Date object's methods, briefly describes the purpose of each, and provides examples of how they are used. Note that the Date object has three static methods - Date.now(), Date.parse() and Date.UTC() - which are always called on the Date object itself. They are not instance methods, and cannot be called on Date object instances created with the Date() constructor.



Index



JavaScript Date Object Methods
MethodDescription

Date()

Syntax:

new Date()
new Date(value)
new Date(dateString)
new Date(dateObject)

new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)

Date()

When called as a constructor (using the new keyword), Date() returns a new Date object. If used with no arguments, the new Date object holds an integer value equal to the number of milliseconds that have elapsed between 00:00:00 UTC on January 1st, 1970 and the current date and time. For example:

const now = new Date();
console.log(now.valueOf());
// 1722574560862
console.log(now)
// Date Fri Aug 02 2024 05:56:00 GMT+0100 (British Summer Time)

Single arguments may consist of a valid timestamp (an integer value in the range -8,640,000,000,000,000 to 8,640,000,000,000,000), a string value representing a date, or another Date object. For example:

const dateOfBirth = new Date("1955-02-25T03:30:00");
console.log(dateOfBirth);
// Date Fri Feb 25 1955 03:30:00 GMT+0000 (Greenwich Mean Time)
console.log(dateOfBirth.valueOf());
// -468621000000

console.log(new Date(-468621000000));
// Date Fri Feb 25 1955 03:30:00 GMT+0000 (Greenwich Mean Time)

console.log(new Date(dateOfBirth));
// Date Fri Feb 25 1955 03:30:00 GMT+0000 (Greenwich Mean Time)

Date() also accepts a comma-delimited series of integer values representing the year, month, day, hours, minutes, seconds and milliseconds. For example:

console.log(new Date(1955, 1, 25, 3, 30));
// Date Fri Feb 25 1955 03:30:00 GMT+0000 (Greenwich Mean Time)

As a minimum requirement, values for the year and month must be supplied (note that months are referenced using a zero-based index, so January is 0, February is 1, and so on). Date and time values can be omitted, starting from the right-hand side. The following combinations of values are all valid:

year, month, day, hour, minute, second, millisecond
year, month, day, hour, minute, second
year, month, day, hour, minute
year, month, day, hour
year, month, day
year, month

When called as a function (i.e. without the new keyword) Date() always returns a string representation of the current date and time. For example:

console.log(Date());
// Fri Aug 02 2024 06:07:36 GMT+0100 (British Summer Time)

Note that the Date() function does not expect to receive any arguments. If arguments are passed to the Date() function, including valid date strings and timestamp values, they will be ignored.

Date.now()

Syntax:

Date.now()

A static method that returns a timestamp value for the current date and time, i.e. the number of milliseconds elapsed since 00:00:00 UTC on January 1st, 1970. For example:

console.log(Date.now()); // 1722575334161

The Date.now() method can be used to create a timestamp without the need to create a Date object, making it an efficient way of finding the time elapsed (in milliseconds) between the current time and some previous event. For example:

const start = Date.now();

console.log("Starting the clock . . .");
// Starting the clock . . .

function someFunc() {
const delay = Date.now() - start;
console.log("Time elapsed: "
+ Math.floor(delay/1000) + " seconds.");
// Time elapsed: 5 seconds.
}

setTimeout(someFunc, 5000);

Date.parse()

Syntax:

Date.parse(dateString)

A static method that parses the string representation of a date and returns a timestamp for that date (an integer value equal to the number of milliseconds that have elapsed between 00:00:00 UTC on January 1st, 1970 and the specified date and time).

Date.parse() accepts strings that comply with the JavaScript date time string format or the IETF standard date format as its argument. Other formats may or may not be supported, depending on the browser implementation. For example:

const someDate = Date.parse("2024-06-06T16:30:00Z");
console.log(someDate);
// 1717691400000
console.log(new Date(someDate));
// Date Thu Jun 06 2024 17:30:00 GMT+0100 (British Summer Time)

const someOtherDate = Date.parse("Thu, 06 Jun 2024 16:30:00 GMT");
console.log(someOtherDate);
// 1717691400000
console.log(new Date(someOtherDate));
// Date Thu Jun 06 2024 17:30:00 GMT+0100 (British Summer Time)

If Date.parse() receives an invalid date string, it will return NaN. For example:

const someDate = Date.parse("2nd March, 2025");
console.log(someDate);
// NaN

If the time zone is not specified in the date string, the time zone offset is taken from the user's system settings. For example:

const someDate = Date.parse("2024-06-06T16:30:00Z");
console.log(someDate);
// 1717691400000
console.log(new Date(someDate));
// Date Thu Jun 06 2024 17:30:00 GMT+0100 (British Summer Time)

const anotherDate = Date.parse("2024-06-06T16:30:00");
console.log(anotherDate);
// 1717687800000
console.log(new Date(anotherDate));
// Date Thu Jun 06 2024 16:30:00 GMT+0100 (British Summer Time)

In the example above, the first date string specifies the time as 16:30 and the time zone as GMT. Because the user's device is set to British Summer Time, which is one hour ahead of GMT, the time displayed on the user's device is GMT+0100, i.e. 17:30. The second date string does not specify a time zone, so the time specified is assumed to be the local time, adjusted for daylight saving.

Date.UTC()

Syntax:

Date.UTC(year)
Date.UTC(year, monthIndex)
Date.UTC(year, monthIndex, day)
Date.UTC(year, monthIndex, day, hour)
Date.UTC(year, monthIndex, day, hour, minute)
Date.UTC(year, monthIndex, day, hour, minute, second)
Date.UTC(year, monthIndex, day, hour, minute, second, millisecond)

A static method that accepts a comma-delimited series of integer date and time values as its arguments and returns a timestamp that represents the number of milliseconds that have elapsed between 00:00:00 UTC on January 1st, 1970 and the date and time specified by those arguments, interpreted as a UTC date time. For example:

const now = Date.UTC(2024, 5, 7, 12, 30, 0);
console.log(now);
// 1717763400000
console.log(new Date(now));
// Date Fri Jun 07 2024 13:30:00 GMT+0100 (British Summer Time)

As a minimum requirement, a value for year must be supplied (note that months are referenced using a zero-based index, so January is 0, February is 1, and so on). Date and time values can be omitted, starting from the right-hand side. The following combinations of values are all valid:

year, month, day, hour, minute, second, millisecond
year, month, day, hour, minute, second
year, month, day, hour, minute
year, month, day, hour
year, month, day
year, month
year

With the exception of year, all other missing values default to 0 except day, which defaults to 1. For example:

const NewYear2024 = Date.UTC(2024);
console.log(NewYear2024);
// 1704067200000
console.log(new Date(NewYear2024));
// Date Mon Jan 01 2024 00:00:00 GMT+0000 (Greenwich Mean Time)

If no arguments are supplied, or if the date they represent is invalid, Date.UTC() returns NaN.

getDate()

Syntax:

getDate()

An instance method that returns an integer in the range 1 to 31 that represents the day of the month according to local time for the Date object on which it is called. For example:

const midSummer2024 = new Date("2024-06-20");
console.log(midSummer2024.getDate());
// 20

Returns NaN if the date is invalid, defaults to 1.

getDay()

Syntax:

getDay()

An instance method that returns an integer in the range 0 to 6 that represents the day of the week according to local time (0 = Sunday, 1 = Monday, 2 = Tuesday, and so on) for the Date object on which it is called. For example:

const weekDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const midSummer2024 = new Date("2024-06-20");
console.log(weekDays[midSummer2024.getDay()]);
// Thursday

Returns NaN if the date is invalid.

getFullYear()

Syntax:

getFullYear()

An instance method that returns an integer value that represents the year according to local time for the Date object on which it is called. For example:

const dateOfBirth = new Date("1955-02-25");
console.log(dateOfBirth.getFullYear());
// 1955

Returns NaN if the date is invalid. This method should be used in preference to the now-deprecated getYear() method.

getHours()

Syntax:

getHours()

An instance method that returns an integer value in the range 0 to 23 that represents the hours according to local time for the Date object on which it is called. For example:

const someDate = new Date("2024-06-08T10:35");
console.log(someDate.getHours());
// 10

Returns NaN if the date is invalid.

getMilliseconds()

Syntax:

getMilliseconds()

An instance method that returns an integer value in the range 0 to 999 that represents the milliseconds according to local time for the Date object on which it is called. For example:

const someDate = new Date("2024-06-09T11:38:45.750");
console.log(someDate.getMilliseconds());
// 750

Returns NaN if the date is invalid.

getMinutes()

Syntax:

getMinutes()

An instance method that returns an integer value in the range 0 to 59 that represents the minutes according to local time for the Date object on which it is called. For example:

const someDate = new Date("2024-06-09T15:25");
console.log(someDate.getMinutes());
// 25

Returns NaN if the date is invalid.

getMonth()

Syntax:

getMonth()

An instance method that returns an integer value in the range 0 to 11 that represents the month according to local time for the Date object on which it is called. For example:

const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const someDate = new Date("2024-06-09");
console.log(someDate.getMonth());
// 5
console.log(months[someDate.getMonth()]);
// June

Note that months are numbered starting from zero, so we have 0 for January, 1 for February, 3 for March, and so on. Returns NaN if the date is invalid.

getSeconds()

Syntax:

getSeconds()

An instance method that returns an integer value in the range 0 to 59 that represents the seconds according to local time for the Date object on which it is called. For example:

const someDate = new Date("2024-06-09T17:05:30");
console.log(someDate.getSeconds());
// 30

Returns NaN if the date is invalid.

getTime()

Syntax:

getTime()

An instance method, functionally equivalent to the valueOf() method, that returns an integer value that represents the number of milliseconds that have elapsed between the epoch (00:00:00 UTC on January 1st, 1970) and the date and time specified by the Date object on which it is called. Otherwise known as a timestamp. For example:

const xmas2023 = new Date("2023-12-25");
console.log(xmas2023.getTime());
// 1703462400000

Returns NaN if the date is invalid.

getTimezoneOffset()

Syntax:

getTimezoneOffset()

An instance method that returns an integer value that represents the difference, in minutes, between the local time represented by the Date object on which it is called and the UTC time represented by that Date object. For example:

const someDate = new Date("2024-06-12");
console.log(someDate);
// Date Wed Jun 12 2024 01:00:00 GMT+0100 (British Summer Time)
console.log(someDate.getTimezoneOffset());
// -60

Returns NaN if the date is invalid.

Local time is dependent on the time zone settings on the user's device. The value returned is negative if the local time zone is ahead of UTC and positive if the local time zone is behind UTC. The time zone offset may be different at different times of the year if Daylight Saving Time (DST) is used in the local time zone.

getYear()

Deprecated - do not use.

Syntax:

getYear()

An instance method that returns an integer value that represents the year according to local time for the Date object on which it is called minus 1900.

For years in the range 1900 to 1999, the value returned is in the range 0 to 99. For years less than 1900, the value returned will be negative.

For years starting at 2000 and above the value returned will be 100 or greater. For example:

const someDate = new Date("2024-06-12");
console.log(someDate.getFullYear());
// 2024
console.log(someDate.getYear());
// 124

Returns NaN if the date is invalid.

This method is deprecated and should not be used - use getFullYear() instead in new code, and if possible replace getYear() with getFullYear() in existing code.

getUTCDate()

Syntax:

getUTCDate()

An instance method that returns an integer in the range 1 to 31 that represents the day of the month according to universal time for the Date object on which it is called. For example:

const someDate = new Date("2024-06-12T00:00");
console.log(someDate);
// Date Wed Jun 12 2024 00:00:00 GMT+0100 (British Summer Time)
console.log(someDate.getDate());
// 12
console.log(someDate.getUTCDate());
// 11

Returns NaN if the date is invalid.

getUTCDay()

Syntax:

getUTCDay()

An instance method that returns an integer in the range 0 to 6 that represents the day of the week according to universal time (0 = Sunday, 1 = Monday, 2 = Tuesday, and so on) for the Date object on which it is called. For example:

const weekDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const someDate = new Date("2024-06-12T00:00");
console.log(weekDays[someDate.getDay()]);
// Wednesday
console.log(weekDays[someDate.getUTCDay()]);
// Tuesday

Returns NaN if the date is invalid.

getUTCFullYear()

Syntax:

getUTCFullYear()

An instance method that returns an integer value that represents the year according to universal time for the Date object on which it is called. For example:

const someDate = new Date("Jan 01 2024 00:00");
console.log(someDate);
// Date Mon Jan 01 2024 00:00:00 GMT+0100 (Central European Standard Time)
console.log(someDate.getFullYear());
// 2024
console.log(someDate.getUTCFullYear());
// 2023

Returns NaN if the date is invalid.

getUTCHours()

Syntax:

getUTCHours()

An instance method that returns an integer value in the range 0 to 23 that represents the hours according to universal time for the Date object on which it is called. For example:

const someDate = new Date("Jan 01 2024 00:00");
console.log(someDate);
// Date Mon Jan 01 2024 00:00:00 GMT+0100 (Central European Standard Time)
console.log(someDate.getHours());
// 0
console.log(someDate.getUTCHours());
// 23

Returns NaN if the date is invalid.

getUTCMilliseconds()

Syntax:

getUTCMilliseconds()

An instance method that returns an integer value in the range 0 to 999 that represents the milliseconds according to universal time for the Date object on which it is called. For example:

const someDate = new Date("2024-06-12T10:35:42.625");
console.log(someDate.getUTCMilliseconds()); // 625

Returns NaN if the date is invalid.

getUTCMinutes()

Syntax:

getUTCMinutes()

An instance method that returns an integer value in the range 0 to 59 that represents the minutes according to universal time for the Date object on which it is called. For example:

const someDate = new Date("2024-06-12T10:35");
console.log(someDate.getUTCMinutes()); // 35

Returns NaN if the date is invalid.

getUTCMonth()

Syntax:

getUTCMonth()

An instance method that returns an integer value in the range 0 to 11 that represents the month according to universal time for the Date object on which it is called. For example:

const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const someDate = new Date("2024-06-09");
console.log(someDate.getUTCMonth());
// 5
console.log(months[someDate.getUTCMonth()]);
// June

Note that months are numbered starting from zero, so we have 0 for January, 1 for February, 3 for March, and so on. Returns NaN if the date is invalid.

getUTCSeconds()

Syntax:

getUTCSeconds()

An instance method that returns an integer value in the range 0 to 59 that represents the seconds according to universal time for the Date object on which it is called. For example:

const someDate = new Date("2024-06-09T10:55:38");
console.log(someDate.getUTCSeconds());
// 38

Returns NaN if the date is invalid.

setDate()

Syntax:

setDate(dateValue)

An instance method that changes the day of the month for the Date object on which it is called to the integer value passed to it as an argument, according to local time. Returns a new timestamp for the Date object or NaN if the value passed to setDate() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2024-06-06");
console.log(someDate.setDate(7));
// 1717718400000
console.log(someDate);
// Date Fri Jun 07 2024 01:00:00 GMT+0100 (British Summer Time)
console.log(someDate.setDate(NaN));
// NaN
console.log(someDate);
// Invalid Date

If the number supplied as the argument to setDate() is outside the expected range, the Date object will be updated accordingly. If a value of 0 is supplied, the date will be set to the last day of the previous month. For example:

const someDate = new Date("2024-06-06");
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)
someDate.setDate(0);
console.log(someDate);
// Date Fri May 31 2024 01:00:00 GMT+0100 (British Summer Time)

For a positive value outside the expected range, the date is advanced by that number of days minus the number of days in the month originally specified. For example:

const someDate = new Date("2024-06-06");
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)
someDate.setDate(45);
console.log(someDate);
// Date Mon Jul 15 2024 01:00:00 GMT+0100 (British Summer Time)

For a negative value outside the expected range, the date is set to whatever date occurs that number of days before the last day of the previous month. For example:

const someDate = new Date("2024-06-06");
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)
someDate.setDate(-15);
console.log(someDate);
// Date Thu May 16 2024 01:00:00 GMT+0100 (British Summer Time)

setFullYear()

Syntax:

setFullYear(yearValue)
setFullYear(yearValue, monthValue)
setFullYear(yearValue, monthValue, dateValue)

An instance method that can be used to change the year, month, and date for the Date object on which it is called depending on the values passed to it as arguments, according to local time.

The argument for the year is an integer representing the year, e.g. 2024.

The month value is optional and is an integer value in the range 0 to 11, where 0 represents January, 1 represents February, 2 represents March, and so on.

The date value is also optional, and is an integer value in the range 1 to 31 representing the day of the month. Note that the date value can only be specified if the month value has also been specified.

Returns a new timestamp for the Date object or NaN if one or more value passed to setFullYear() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setFullYear(2024);
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)
someDate.setFullYear(2024,6);
console.log(someDate);
// Date Sat Jul 06 2024 01:00:00 GMT+0100 (British Summer Time)
someDate.setFullYear(2024,6,15);
console.log(someDate);
// Date Mon Jul 15 2024 01:00:00 GMT+0100 (British Summer Time)
console.log(someDate.setFullYear(2024,6,NaN));
// NaN
console.log(someDate);
// Invalid Date

If any of the numbers supplied as arguments to setFullYear() are outside the expected range, the Date object will be updated accordingly. For positive month values outside the expected range (0-11), the date is advanced by that number of months minus the index value of the month originally specified (months are indexed from 0). For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setFullYear(2023,17,6);
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)

If a negative value is supplied for month, the date is decremented by subtracting that number of months minus one (because months are indexed from 0) from the end of the year immediately preceding the specified year. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setFullYear(2023,-6,6);
console.log(someDate);
// Date Wed Jul 06 2022 01:00:00 GMT+0100 (British Summer Time)

For positive date values outside the expected range (0-31), the date is advanced by that number of days minus the number of days in the specified month. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setFullYear(2023,5,45);
console.log(someDate);
// Date Sat Jul 15 2023 01:00:00 GMT+0100 (British Summer Time)

If a negative value is supplied for date, the date is decremented by subtracting that number of days from the end of the month immediately preceding the month specified. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setFullYear(2023,5,-15);
console.log(someDate);
// Date Tue May 16 2023 01:00:00 GMT+0100 (British Summer Time)

If no values are supplied for the month and date arguments, the existing values usually remain unchanged. This would only not be true in exceptional circumstances, such as where the date originally specified is the last day of February in a leap year. For example:

const someDate = new Date("2024-02-29");
console.log(someDate);
// Date Thu Feb 29 2024 00:00:00 GMT+0000 (Greenwich Mean Time)
someDate.setFullYear(2023);
console.log(someDate);
// Date Wed Mar 01 2023 00:00:00 GMT+0000 (Greenwich Mean Time)

setHours()

Syntax:

setHours(hoursValue) setHours(hoursValue, minutesValue)
setHours(hoursValue, minutesValue, secondsValue)
setHours(hoursValue, minutesValue, secondsValue, msValue)

An instance method that can be used to change the hours, minutes, seconds and milliseconds for the Date object on which it is called, depending on the values passed to it as arguments, according to local time.

The hours value is required, and is an integer value in the range 0 to 23 representing the number of complete hours that have elapsed since midnight.

The minutes value is optional, and is an integer value in the range 0 to 59 representing the number of complete minutes that have elapsed since the start of the current hour. Note that the minutes value can only be specified if the hours value has also been specified.

The seconds value is also optional, and is an integer value in the range 0 to 59 representing the number of complete seconds that have elapsed since the start of the current minute. Note that the seconds value can only be specified if the minutes value has also been specified.

The milliseconds value is also optional, and is an integer value in the range 0 to 999 representing the number of complete milliseconds that have elapsed since the start of the current second. Note that the milliseconds value can only be specified if the seconds value has also been specified.

Returns a new timestamp for the Date object or NaN if one or more value passed to setHours() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2024-06-06");
console.log(someDate.valueOf());
// 1717632000000
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)
let timeStamp = someDate.setHours(12);
console.log(timeStamp);
// 1717671600000
console.log(someDate);
// Date Thu Jun 06 2024 12:00:00 GMT+0100 (British Summer Time)v timeStamp = someDate.setHours(6,30);
console.log(timeStamp);
// 1717651800000
console.log(someDate);
// Date Thu Jun 06 2024 06:30:00 GMT+0100 (British Summer Time)
timeStamp = someDate.setHours(8,45,30);
console.log(timeStamp);
// 1717659930000
console.log(someDate);
// Date Thu Jun 06 2024 08:45:30 GMT+0100 (British Summer Time)
timeStamp = someDate.setHours(8,45,30,750);
console.log(timeStamp);
// 1717659930750
console.log(someDate);
// Date Thu Jun 06 2024 08:45:30 GMT+0100 (British Summer Time)
timeStamp = someDate.setHours(6,NaN);
console.log(timeStamp);
// NaN
console.log(someDate);
// Invalid Date

If any of the numbers supplied as arguments to setHours() are outside the expected range, the Date object will be updated accordingly. A positive value outside the expected range will increment the time values (and possibly the date value). For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setHours(12,35,75);
console.log(timeStamp);
// 1717673775000
console.log(someDate);
// Date Thu Jun 06 2024 12:36:15 GMT+0100 (British Summer Time)
timeStamp = someDate.setHours(30);
console.log(timeStamp);
// 1717738575000
console.log(someDate);
// Date Fri Jun 07 2024 06:36:15 GMT+0100 (British Summer Time)

If any of the arguments supplied to setHour() are negative values, the time values (and possibly the date value) will be decremented. A negative value for milliseconds will set the current milliseconds value to zero, and then decrement the new time value by that number of milliseconds. For example:

const someDate = new Date("2024-06-06T12:35:45.500");
console.log(someDate.valueOf());
// 1717673745500
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setHours(12,35,45,-2000);
console.log(timeStamp);
// 1717673743000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:43 GMT+0100 (British Summer Time)

A negative value for seconds will set the current seconds value to zero, and then decrement the new time value by that number of seconds. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setHours(12,35,-90);
console.log(timeStamp);
// 1717673610000
console.log(someDate);
// Date Thu Jun 06 2024 12:33:30 GMT+0100 (British Summer Time)

A negative value for minutes will set the current minutes value to zero, and then decrement the time value by that number of minutes. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setHours(12,-120);v console.log(timeStamp);
// 1717664445000
console.log(someDate);
// Date Thu Jun 06 2024 10:00:45 GMT+0100 (British Summer Time)

A negative value for hours will set the current hours value to zero, and then decrement the existing time value by that number of hours. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setHours(-72);
console.log(timeStamp);
// 1717371345000
console.log(someDate);
// Date Mon Jun 03 2024 00:35:45 GMT+0100 (British Summer Time)

Note that existing datetime values to the right of the rightmost value set using setHour() are unaffected. For example, if a new value for hours is set using setHour(), and no other values are specified, the existing values for minutes, seconds and milliseconds remain unchanged.

setMilliseconds()

Syntax:

setMilliseconds(millisecondsValue)

An instance method that can be used to change the milliseconds for the Date object on which it is called, depending on the millisecond value passed to it as an argument, according to local time.

The milliseconds value is required, and is an integer value in the range 0 to 999 representing the number of complete milliseconds that have elapsed since the start of the current minute.

Returns a new timestamp for the Date object or NaN if the value passed to setMilliseconds() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setMilliseconds(750);
console.log(timeStamp);
// 1717673745750
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
timeStamp = someDate.setMilliseconds(NaN);
console.log(timeStamp);
// NaN
console.log(someDate);
// Invalid Date

If the value supplied as the argument to setMilliseconds() is outside the expected range, the Date object will be updated accordingly. A positive value outside the expected range will increment the time values. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setMilliseconds(5000);
console.log(timeStamp);
// 1717673750000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:50 GMT+0100 (British Summer Time)

If the argument supplied to setMilliseconds() is a negative value, the milliseconds value will be set to zero and the new time value will then be decremented by that number of milliseconds. For example:

const someDate = new Date("2024-06-06T12:35:45.750");
console.log(someDate.valueOf());
// 1717673745750
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setMilliseconds(-5000);
console.log(timeStamp);
// 1717673740000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:40 GMT+0100 (British Summer Time)

setMinutes()

Syntax:

setMinutes(minutesValue)
setMinutes(minutesValue, secondsValue)
setMinutes(minutesValue, secondsValue, msValue)

An instance method that can be used to change the minutes, seconds and milliseconds for the Date object on which it is called, depending on the values passed to it as arguments, according to local time.

The minutes value is required, and is an integer value in the range 0 to 59 representing the number of complete minutes that have elapsed since the beginning of the current hour.

The seconds value is optional, and is an integer value in the range 0 to 59 representing the number of complete seconds that have elapsed since the start of the current minute. Note that the seconds value can only be specified if the minutes value has also been specified.

The milliseconds value is also optional, and is an integer value in the range 0 to 999 representing the number of complete milliseconds that have elapsed since the start of the current second. Note that the milliseconds value can only be specified if the seconds value has also been specified.

Returns a new timestamp for the Date object or NaN if one or more value passed to setMinutes() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2024-06-06T06:30");
console.log(someDate.valueOf());
// 1717651800000
console.log(someDate);
// Date Thu Jun 06 2024 06:30:00 GMT+0100 (British Summer Time)
let timeStamp = someDate.setMinutes(45);
console.log(timeStamp);
// 1717652700000
console.log(someDate);
// Date Thu Jun 06 2024 06:45:00 GMT+0100 (British Summer Time)
timeStamp = someDate.setMinutes(45,30);
console.log(timeStamp);
// 1717652730000
console.log(someDate);
// Date Thu Jun 06 2024 06:45:30 GMT+0100 (British Summer Time)
timeStamp = someDate.setMinutes(45,30,500);
console.log(timeStamp);
// 1717652730500
console.log(someDate);
// Date Thu Jun 06 2024 06:45:30 GMT+0100 (British Summer Time)
timeStamp = someDate.setMinutes(45,30,NaN);
console.log(timeStamp);
// NaN
console.log(someDate);
// Invalid Date

If any of the numbers supplied as arguments to setMinutes() are outside the expected range, the Date object will be updated accordingly. A positive value outside the expected range will increment the time values (and possibly the date value). For example:

const someDate = new Date("2024-12-31T23:35:45");
console.log(someDate.valueOf());
// 1735688145000
console.log(someDate);
// Date Tue Dec 31 2024 23:35:45 GMT+0000 (Greenwich Mean Time)
let timeStamp = someDate.setMinutes(35,75);
console.log(timeStamp);
// 1735688175000
console.log(someDate);
// Date Tue Dec 31 2024 23:36:15 GMT+0000 (Greenwich Mean Time)
timeStamp = someDate.setMinutes(90);
console.log(timeStamp);
// 1735691415000
console.log(someDate);
// Date Wed Jan 01 2025 00:30:15 GMT+0000 (Greenwich Mean Time)

If any of the arguments supplied to setMinutes() are negative values, the time values (and possibly the date value) will be decremented accordingly. A negative value for milliseconds will set the current milliseconds value to zero, and then decrement the new time value by that number of milliseconds. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setMinutes(35,45,-2000);
console.log(timeStamp);
// 1717673743000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:43 GMT+0100 (British Summer Time)

A negative value for seconds will set the current seconds value to zero, and then decrement the new time value by that number of seconds. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setMinutes(35,-120);
console.log(timeStamp);
// 1717673580000
console.log(someDate);
// Date Thu Jun 06 2024 12:33:00 GMT+0100 (British Summer Time)

A negative value for minutes will set the current minutes value to zero, and then decrement the time value by that number of minutes. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setMinutes(-90);
console.log(timeStamp);
// 1717666245000
console.log(someDate);
// Date Thu Jun 06 2024 10:30:45 GMT+0100 (British Summer Time)

Note that existing datetime values to the right of the rightmost value set using setMinutes() are unaffected. For example, if a new value for minutes is set using setMinutes(), and no other values are specified, the existing values for seconds and milliseconds remain unchanged.

setMonth()

Syntax:

setMonth(monthValue)
setMonth(monthValue, dateValue)

An instance method that can be used to change the month and date for the Date object on which it is called depending on the values passed to it as arguments, according to local time.

The argument for the month is required, and is an integer value in the range 0 to 11, where 0 represents January, 1 represents February, 2 represents March, and so on.

The date value is optional, and is an integer value in the range 1 to 31 representing the day of the month. Note that the date value can only be specified if the month value has also been specified.

Returns a new timestamp for the Date object or NaN if one or more value passed to setMonth() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setMonth(6);
console.log(someDate);
// Date Thu Jul 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setMonth(6,15);
console.log(someDate);
// Date Sat Jul 15 2023 01:00:00 GMT+0100 (British Summer Time)
console.log(someDate.setMonth(6,NaN));
// NaN
console.log(someDate);
// Invalid Date

If any of the numbers supplied as arguments to setMonth() are outside the expected range, the Date object will be updated accordingly. For positive month values outside the expected range (0-11), the date is advanced by that number of months minus the index value of the month originally specified (months are indexed from 0). For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setMonth(17,6);
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)

If a negative value is supplied for month, the date is decremented by subtracting that number of months minus one (because months are indexed from 0) from the end of the year immediately preceding the specified year. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setMonth(-6,6);
console.log(someDate);
// Date Wed Jul 06 2022 01:00:00 GMT+0100 (British Summer Time)

For positive date values outside the expected range (0-31), the date is advanced by that number of days minus the number of days in the specified month. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setMonth(5,45);
console.log(someDate);
// Date Sat Jul 15 2023 01:00:00 GMT+0100 (British Summer Time)

If a negative value is supplied for date, the date is decremented by subtracting that number of days from the end of the month immediately preceding the month specified. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setMonth(5,-15);
console.log(someDate);
// Date Tue May 16 2023 01:00:00 GMT+0100 (British Summer Time)

If no values are supplied for the date argument, the existing values will remain unchanged unless the date value originally specified is greater than 28, in which case the outcome will depend on the difference between the number of days in the originally specified month and the number of days in the month specified by the argument passed to setMonth(). For example:

const someDate = new Date("2024-03-31");
console.log(someDate);
// Date Sun Mar 31 2024 00:00:00 GMT+0000 (Greenwich Mean Time)
someDate.setMonth(1);
console.log(someDate);
// Date Sat Mar 02 2024 00:00:00 GMT+0000 (Greenwich Mean Time)

In the above example, the argument supplied to setMonth() is 1, signifying the month of February, which has 29 days in 2024 (a leap year). Because the original date value is 31, the new date is obtained by adding two days to the last day of February 2024.

setSeconds()

Syntax:

setSeconds(secondsValue)
setSeconds(secondsValue, msValue)

An instance method that can be used to change the seconds and milliseconds for the Date object on which it is called, depending on the values passed to it as arguments, according to local time.

The seconds value is required, and is an integer value in the range 0 to 59 representing the number of complete seconds that have elapsed since the beginning of the current minute.

The milliseconds value is optional, and is an integer value in the range 0 to 999 representing the number of complete milliseconds that have elapsed since the start of the current second. Note that the milliseconds value can only be specified if the seconds value has also been specified.

Returns a new timestamp for the Date object or NaN if one or more value passed to setSeconds() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2024-06-06T06:30");
console.log(someDate.valueOf());
// 1717651800000
console.log(someDate);
// Date Thu Jun 06 2024 06:30:00 GMT+0100 (British Summer Time)
let timeStamp = someDate.setSeconds(45);
console.log(timeStamp);
// 1717651845000
console.log(someDate);
// Date Thu Jun 06 2024 06:30:45 GMT+0100 (British Summer Time)
timeStamp = someDate.setSeconds(45,750);
console.log(timeStamp);
// 1717651845750
console.log(someDate);
// Date Thu Jun 06 2024 06:30:45 GMT+0100 (British Summer Time)
timeStamp = someDate.setSeconds(45,NaN);
console.log(timeStamp);
// NaN
console.log(someDate);
// Invalid Date

If any of the numbers supplied as arguments to setSeconds() are outside the expected range, the Date object will be updated accordingly. A positive value outside the expected range will increment the time values (and possibly the date value). For example:

const someDate = new Date("2024-12-31T23:59:45");
console.log(someDate.valueOf());
// 1735689585000
console.log(someDate);
// Date Tue Dec 31 2024 23:59:45 GMT+0000 (Greenwich Mean Time)
let timeStamp = someDate.setSeconds(59,2000);
console.log(timeStamp);
// 1735689601000
console.log(someDate);
// Date Wed Jan 01 2025 00:00:01 GMT+0000 (Greenwich Mean Time)
timeStamp = someDate.setSeconds(90);
console.log(timeStamp);
// 1735689690000
console.log(someDate);
// Date Wed Jan 01 2025 00:01:30 GMT+0000 (Greenwich Mean Time)

If any of the arguments supplied to setSeconds() are negative values, the time values (and possibly the date value) will be decremented accordingly. A negative value for milliseconds will set the current milliseconds value to zero, and then decrement the new time value by that number of milliseconds. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setSeconds(45,-2000);
console.log(timeStamp);
// 1717673743000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:43 GMT+0100 (British Summer Time)

A negative value for seconds will set the current seconds value to zero, and then decrement the new time value by that number of seconds. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setSeconds(-120);
console.log(timeStamp);
// 1717673580000
console.log(someDate);
// Date Thu Jun 06 2024 12:33:00 GMT+0100 (British Summer Time)

Note that if only the seconds value is set, any existing value for milliseconds remains unchanged.

setTime()

Syntax:

setTime(timeValue)

An instance method that can be used to set or change the timestamp value (the number of milliseconds that have elapsed since 00:00:00 UTC on 1st January 1970) for the Date object on which it is called. For example:

const sol24 = new Date("2024-06-20T22:51");
const copySol24 = new Date();
let timeStamp = sol24.valueOf();
let newTimeStamp = copySol24.setTime(timeStamp);
console.log(newTimeStamp);
// 1718920260000
console.log(copySol24);
// Date Thu Jun 20 2024 22:51:00 GMT+0100 (British Summer Time)

Returns the new timestamp for the Date object or NaN if the value passed to setTime() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date.

setUTCDate()

Syntax:

setUTCDate(dateValue)

An instance method that changes the day of the month for the Date object on which it is called to the integer value passed to it as an argument, according to universal time. Returns a new timestamp for the Date object or NaN if the value passed to setUTCDate() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2024-06-06");
console.log(someDate.setDate(7));
// 1717718400000
console.log(someDate);
// Date Fri Jun 07 2024 01:00:00 GMT+0100 (British Summer Time)
console.log(someDate.setDate(NaN));
// NaN
console.log(someDate);
// Invalid Date

If the number supplied as the argument to setUTCDate() is outside the expected range, the Date object will be updated accordingly. If a value of 0 is supplied, the date will be set to the last day of the previous month. For example:

const someDate = new Date("2024-06-06");
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCDate(0);
console.log(someDate);
// Date Fri May 31 2024 01:00:00 GMT+0100 (British Summer Time)

For a positive value outside the expected range, the date is advanced by that number of days minus the number of days in the month originally specified. For example:

const someDate = new Date("2024-06-06");
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCDate(45);
console.log(someDate);
// Date Mon Jul 15 2024 01:00:00 GMT+0100 (British Summer Time)

For a negative value outside the expected range, the date is set to whatever date occurs that number of days before the last day of the previous month. For example:

const someDate = new Date("2024-06-06");
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCDate(-15);
console.log(someDate);
// Date Thu May 16 2024 01:00:00 GMT+0100 (British Summer Time)

setUTCFullYear()

Syntax:

setUTCFullYear(yearValue)
setUTCFullYear(yearValue, monthValue)
setUTCFullYear(yearValue, monthValue, dateValue)

An instance method that can be used to change the year, month, and date for the Date object on which it is called depending on the values passed to it as arguments, according to universal time.

The argument for the year is an integer representing the year, e.g. 2024.

The month value is optional and is an integer value in the range 0 to 11, where 0 represents January, 1 represents February, 2 represents March, and so on.

The date value is also optional, and is an integer value in the range 1 to 31 representing the day of the month. Note that the date value can only be specified if the month value has also been specified.

Returns a new timestamp for the Date object or NaN if one or more value passed to setUTCFullYear() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCFullYear(2024);
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCFullYear(2024,6);
console.log(someDate);
// Sat Jul 06 2024 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCFullYear(2024,6,15);
console.log(someDate);
// Date Mon Jul 15 2024 01:00:00 GMT+0100 (British Summer Time)
console.log(someDate.setUTCFullYear(2024,6,NaN));
// NaN
console.log(someDate);
// Invalid Date

If any of the numbers supplied as arguments to setUTCFullYear() are outside the expected range, the Date object will be updated accordingly. For positive month values outside the expected range (0-11), the date is advanced by that number of months minus the index value of the month originally specified (months are indexed from 0). For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)v someDate.setUTCFullYear(2023,17,6);
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)

If a negative value is supplied for month, the date is decremented by subtracting that number of months minus one (because months are indexed from 0) from the end of the year immediately preceding the specified year. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCFullYear(2023,-6,6);
console.log(someDate);
// Date Wed Jul 06 2022 01:00:00 GMT+0100 (British Summer Time)

For positive date values outside the expected range (0-31), the date is advanced by that number of days minus the number of days in the specified month. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCFullYear(2023,5,45);
console.log(someDate);
// Date Sat Jul 15 2023 01:00:00 GMT+0100 (British Summer Time)

If a negative value is supplied for date, the date is decremented by subtracting that number of days from the end of the month immediately preceding the month specified. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCFullYear(2023,5,-15);
console.log(someDate);
// Date Tue May 16 2023 01:00:00 GMT+0100 (British Summer Time)

If no values are supplied for the month and date arguments, the existing values usually remain unchanged. This would only not be true in exceptional circumstances, such as where the date originally specified is the last day of February in a leap year. For example:

const someDate = new Date("2024-02-29");
console.log(someDate);
// Date Thu Feb 29 2024 00:00:00 GMT+0000 (Greenwich Mean Time)
someDate.setUTCFullYear(2023);
console.log(someDate);
// Date Wed Mar 01 2023 00:00:00 GMT+0000 (Greenwich Mean Time)

setUTCHours()

Syntax:

setUTCHours(hoursValue)
setUTCHours(hoursValue, minutesValue)
setUTCHours(hoursValue, minutesValue, secondsValue)
setUTCHours(hoursValue, minutesValue, secondsValue, msValue)

An instance method that can be used to change the hours, minutes, seconds and milliseconds for the Date object on which it is called, depending on the values passed to it as arguments, according to universal time.

The hours value is required, and is an integer value in the range 0 to 23 representing the number of complete hours that have elapsed since midnight.

The minutes value is optional, and is an integer value in the range 0 to 59 representing the number of complete minutes that have elapsed since the start of the current hour. Note that the minutes value can only be specified if the hours value has also been specified.

The seconds value is also optional, and is an integer value in the range 0 to 59 representing the number of complete seconds that have elapsed since the start of the current minute. Note that the seconds value can only be specified if the minutes value has also been specified.

The milliseconds value is also optional, and is an integer value in the range 0 to 999 representing the number of complete milliseconds that have elapsed since the start of the current second. Note that the milliseconds value can only be specified if the seconds value has also been specified.

Returns a new timestamp for the Date object or NaN if one or more value passed to setUTCHours() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2024-06-06");
console.log(someDate.valueOf());
// 1717632000000
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCHours(12);
console.log(timeStamp);
// 1717675200000
console.log(someDate);
// Date Thu Jun 06 2024 13:00:00 GMT+0100 (British Summer Time)
timeStamp = someDate.setUTCHours(6,30);
console.log(timeStamp);
// 1717655400000
console.log(someDate);
// Date Thu Jun 06 2024 07:30:00 GMT+0100 (British Summer Time)
timeStamp = someDate.setUTCHours(8,45,30);
console.log(timeStamp);
// 1717663530000
console.log(someDate);
// Date Thu Jun 06 2024 09:45:30 GMT+0100 (British Summer Time)
timeStamp = someDate.setUTCHours(8,45,30,750);
console.log(timeStamp);
// 1717663530750
console.log(someDate);
// Date Thu Jun 06 2024 09:45:30 GMT+0100 (British Summer Time)
timeStamp = someDate.setUTCHours(6,NaN);
console.log(timeStamp);
// NaN
console.log(someDate);
// Invalid Date

If any of the numbers supplied as arguments to setUTCHours() are outside the expected range, the Date object will be updated accordingly. A positive value outside the expected range will increment the time values (and possibly the date value). For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCHours(12,35,75);
console.log(timeStamp);
// 1717677375000
console.log(someDate);
// Date Thu Jun 06 2024 13:36:15 GMT+0100 (British Summer Time)
timeStamp = someDate.setUTCHours(30);
console.log(timeStamp);
// 1717742175000
console.log(someDate);
// Date Fri Jun 07 2024 07:36:15 GMT+0100 (British Summer Time)

If any of the arguments supplied to setUTCHours() are negative values, the time values (and possibly the date value) will be decremented. A negative value for milliseconds will set the current milliseconds value to zero, and then decrement the new time value by that number of milliseconds. For example:

const someDate = new Date("2024-06-06T12:35:45.500");
console.log(someDate.valueOf());
// 1717673745500
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCHours(12,35,45,-2000);
console.log(timeStamp);
// 1717677343000
console.log(someDate);
// Date Thu Jun 06 2024 13:35:43 GMT+0100 (British Summer Time)

A negative value for seconds will set the current seconds value to zero, and then decrement the new time value by that number of seconds. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCHours(12,35,-90);
console.log(timeStamp);
// 1717677210000
console.log(someDate);
// Date Thu Jun 06 2024 13:33:30 GMT+0100 (British Summer Time)

A negative value for minutes will set the current minutes value to zero, and then decrement the time value by that number of minutes. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCHours(12,-120);
console.log(timeStamp);
// 1717668045000
1717668045000
console.log(someDate);
// Date Thu Jun 06 2024 11:00:45 GMT+0100 (British Summer Time)

A negative value for hours will set the current hours value to zero, and then decrement the existing time value by that number of hours. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCHours(-72);
console.log(timeStamp);
// 1717374945000
console.log(someDate);
// Date Mon Jun 03 2024 01:35:45 GMT+0100 (British Summer Time)

Note that existing datetime values to the right of the rightmost value set using setUTCHours() are unaffected. For example, if a new value for hours is set using setUTCHours(), and no other values are specified, the existing values for minutes, seconds and milliseconds remain unchanged.

setUTCMilliseconds()

Syntax:

setUTCMilliseconds(millisecondsValue)

An instance method that can be used to change the milliseconds for the Date object on which it is called, depending on the millisecond value passed to it as an argument, according to universal time.

The milliseconds value is required, and is an integer value in the range 0 to 999 representing the number of complete milliseconds that have elapsed since the start of the current minute.

Returns a new timestamp for the Date object or NaN if the value passed to setUTCMilliseconds() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCMilliseconds(750);
console.log(timeStamp);
// 1717673745750
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
timeStamp = someDate.setUTCMilliseconds(NaN);
console.log(timeStamp);
// NaN
console.log(someDate);
// Invalid Date

If the argument supplied to setUTCMilliseconds() is outside the expected range, the Date object will be updated accordingly. A positive value outside the expected range will increment the time values. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCMilliseconds(5000);
console.log(timeStamp);
// 1717673750000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:50 GMT+0100 (British Summer Time)

If the argument supplied to setUTCMilliseconds() is a negative value, the milliseconds value will be set to zero and the new time value will then be decremented by that number of milliseconds. For example:

const someDate = new Date("2024-06-06T12:35:45.750");
console.log(someDate.valueOf());
// 1717673745750
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCMilliseconds(-5000);
console.log(timeStamp);
// 1717673740000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:40 GMT+0100 (British Summer Time)

setUTCMinutes()

Syntax:

setUTCMinutes(minutesValue)
setUTCMinutes(minutesValue, secondsValue)
setUTCMinutes(minutesValue, secondsValue, msValue)

An instance method that can be used to change the minutes, seconds and milliseconds for the Date object on which it is called, depending on the values passed to it as arguments, according to universal time.

The minutes value is required, and is an integer value in the range 0 to 59 representing the number of complete minutes that have elapsed since the beginning of the current hour.

The seconds value is optional, and is an integer value in the range 0 to 59 representing the number of complete seconds that have elapsed since the start of the current minute. Note that the seconds value can only be specified if the minutes value has also been specified.

The milliseconds value is also optional, and is an integer value in the range 0 to 999 representing the number of complete milliseconds that have elapsed since the start of the current second. Note that the milliseconds value can only be specified if the seconds value has also been specified.

Returns a new timestamp for the Date object or NaN if one or more value passed to setUTCMinutes() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2024-06-06T06:30");
console.log(someDate.valueOf());
// 1717651800000
console.log(someDate);
// Date Thu Jun 06 2024 06:30:00 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCMinutes(45);
console.log(timeStamp);
// 1717652700000
console.log(someDate);
// Date Thu Jun 06 2024 06:45:00 GMT+0100 (British Summer Time)
timeStamp = someDate.setUTCMinutes(45,30);
console.log(timeStamp);
// 1717652730000
console.log(someDate);
// Date Thu Jun 06 2024 06:45:30 GMT+0100 (British Summer Time)
timeStamp = someDate.setUTCMinutes(45,30,500);
console.log(timeStamp);
// 1717652730500
console.log(someDate);
// Date Thu Jun 06 2024 06:45:30 GMT+0100 (British Summer Time)
timeStamp = someDate.setUTCMinutes(45,30,NaN);
console.log(timeStamp);
// NaN
console.log(someDate);
// Invalid Date

If any of the numbers supplied as arguments to setUTCMinutes() are outside the expected range, the Date object will be updated accordingly. A positive value outside the expected range will increment the time values (and possibly the date value). For example:

const someDate = new Date("2024-12-31T23:35:45");
console.log(someDate.valueOf());
// 1735688145000
console.log(someDate);
// Date Tue Dec 31 2024 23:35:45 GMT+0000 (Greenwich Mean Time)
let timeStamp = someDate.setUTCMinutes(35,75);
console.log(timeStamp);
// 1735688175000
console.log(someDate);
// Date Tue Dec 31 2024 23:36:15 GMT+0000 (Greenwich Mean Time)
timeStamp = someDate.setUTCMinutes(90);
console.log(timeStamp);
// 1735691415000
console.log(someDate);
// Date Wed Jan 01 2025 00:30:15 GMT+0000 (Greenwich Mean Time)

If any of the arguments supplied to setUTCMinutes() are negative values, the time values (and possibly the date value) will be decremented accordingly. A negative value for milliseconds will set the current milliseconds value to zero, and then decrement the new time value by that number of milliseconds. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCMinutes(35,45,-2000);
console.log(timeStamp);
// 1717673743000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:43 GMT+0100 (British Summer Time)

A negative value for seconds will set the current seconds value to zero, and then decrement the new time value by that number of seconds. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCMinutes(35,-120);
console.log(timeStamp);
// 1717673580000
console.log(someDate);
// Date Thu Jun 06 2024 12:33:00 GMT+0100 (British Summer Time)

A negative value for minutes will set the current minutes value to zero, and then decrement the time value by that number of minutes. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCMinutes(-90);
console.log(timeStamp);
// 1717666245000
console.log(someDate);
// Date Thu Jun 06 2024 10:30:45 GMT+0100 (British Summer Time)

Note that existing datetime values to the right of the rightmost value set using setUTCMinutes() are unaffected. For example, if a new value for minutes is set using setUTCMinutes(), and no other values are specified, the existing values for seconds and milliseconds remain unchanged.

setUTCMonth()

Syntax:

setUTCMonth(monthValue)
setUTCMonth(monthValue, dateValue)

An instance method that can be used to change the month and date for the Date object on which it is called depending on the values passed to it as arguments, according to universal time.

The argument for the month is required, and is an integer value in the range 0 to 11, where 0 represents January, 1 represents February, 2 represents March, and so on.

The date value is optional, and is an integer value in the range 1 to 31 representing the day of the month. Note that the date value can only be specified if the month value has also been specified.

Returns a new timestamp for the Date object or NaN if one or more value passed to setUTCMonth() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCMonth(6);
console.log(someDate);
// Date Thu Jul 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCMonth(6,15);
console.log(someDate);
// Date Sat Jul 15 2023 01:00:00 GMT+0100 (British Summer Time)
console.log(someDate.setMonth(6,NaN));
// NaN
console.log(someDate);
// Invalid Date

If any of the numbers supplied as arguments to setUTCMonth() are outside the expected range, the Date object will be updated accordingly. For positive month values outside the expected range (0-11), the date is advanced by that number of months minus the index value of the month originally specified (months are indexed from 0). For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCMonth(17,6);
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)

If a negative value is supplied for month, the date is decremented by subtracting that number of months minus one (because months are indexed from 0) from the end of the year immediately preceding the specified year. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCMonth(-6,6);
console.log(someDate);
// Date Wed Jul 06 2022 01:00:00 GMT+0100 (British Summer Time)

For positive date values outside the expected range (0-31), the date is advanced by that number of days minus the number of days in the specified month. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCMonth(5,45);
console.log(someDate);
// Date Sat Jul 15 2023 01:00:00 GMT+0100 (British Summer Time)

If a negative value is supplied for date, the date is decremented by subtracting that number of days from the end of the month immediately preceding the month specified. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setUTCMonth(5,-15);
console.log(someDate);
// Date Tue May 16 2023 01:00:00 GMT+0100 (British Summer Time)

If no values are supplied for the date argument, the existing values will remain unchanged unless the date value originally specified is greater than 28, in which case the outcome will depend on the difference between the number of days in the originally specified month and the number of days in the month specified by the argument passed to setUTCMonth(). For example:

const someDate = new Date("2024-03-31");
console.log(someDate);
// Date Sun Mar 31 2024 00:00:00 GMT+0000 (Greenwich Mean Time)
someDate.setUTCMonth(1);
console.log(someDate);
// Date Sat Mar 02 2024 00:00:00 GMT+0000 (Greenwich Mean Time)

In the above example, the argument supplied to setUTCMonth() is 1, signifying the month of February, which has 29 days in 2024 (a leap year). Because the original date value is 31, the new date is obtained by adding two days to the last day of February 2024.

setUTCSeconds()

Syntax:

setUTCSeconds(secondsValue)
setUTCSeconds(secondsValue, msValue)

An instance method that can be used to change the seconds and milliseconds for the Date object on which it is called, depending on the values passed to it as arguments, according to universal time.

The seconds value is required, and is an integer value in the range 0 to 59 representing the number of complete seconds that have elapsed since the beginning of the current minute.

The milliseconds value is optional, and is an integer value in the range 0 to 999 representing the number of complete milliseconds that have elapsed since the start of the current second. Note that the milliseconds value can only be specified if the seconds value has also been specified.

Returns a new timestamp for the Date object or NaN if one or more value passed to setUTCSeconds() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2024-06-06T06:30");
console.log(someDate.valueOf());
// 1717651800000
console.log(someDate);
// Date Thu Jun 06 2024 06:30:00 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCSeconds(45);
console.log(timeStamp);
// 1717651845000
console.log(someDate);
// Date Thu Jun 06 2024 06:30:45 GMT+0100 (British Summer Time)
timeStamp = someDate.setUTCSeconds(45,750);
console.log(timeStamp);
// 1717651845750
console.log(someDate);
// Date Thu Jun 06 2024 06:30:45 GMT+0100 (British Summer Time)
timeStamp = someDate.setUTCSeconds(45,NaN);
console.log(timeStamp);
// NaN
console.log(someDate);
// Invalid Date

If any of the numbers supplied as arguments to setUTCSeconds() are outside the expected range, the Date object will be updated accordingly. A positive value outside the expected range will increment the time values (and possibly the date value). For example:

const someDate = new Date("2024-12-31T23:59:45");
console.log(someDate.valueOf());
// 1735689585000
console.log(someDate);
// Date Tue Dec 31 2024 23:59:45 GMT+0000 (Greenwich Mean Time)
let timeStamp = someDate.setUTCSeconds(59,2000);
console.log(timeStamp);
// 1735689601000
console.log(someDate);
// Date Wed Jan 01 2025 00:00:01 GMT+0000 (Greenwich Mean Time)
timeStamp = someDate.setUTCSeconds(90);
console.log(timeStamp);
// 1735689690000
console.log(someDate);
// Date Wed Jan 01 2025 00:01:30 GMT+0000 (Greenwich Mean Time)

If any of the arguments supplied to setUTCSeconds() are negative values, the time values (and possibly the date value) will be decremented accordingly. A negative value for milliseconds will set the current milliseconds value to zero, and then decrement the new time value by that number of milliseconds. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCSeconds(45,-2000);
console.log(timeStamp); // 1717673743000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:43 GMT+0100 (British Summer Time)

A negative value for seconds will set the current seconds value to zero, and then decrement the new time value by that number of seconds. For example:

const someDate = new Date("2024-06-06T12:35:45");
console.log(someDate.valueOf());
// 1717673745000
console.log(someDate);
// Date Thu Jun 06 2024 12:35:45 GMT+0100 (British Summer Time)
let timeStamp = someDate.setUTCSeconds(-120);
console.log(timeStamp);
// 1717673580000
console.log(someDate);
// Date Thu Jun 06 2024 12:33:00 GMT+0100 (British Summer Time)

Note that if only the seconds value is set, any existing value for milliseconds remains unchanged.

setYear()

Deprecated - do not use.

Syntax:

setYear(yearValue)

An instance method that can be used to change the year for the Date object on which it is called depending on the value passed to it as an argument, according to local time. The argument is an integer representing the year, e.g. 2024.

Returns a new timestamp for the Date object or NaN if the value passed to setYear() is NaN or another value that is coerced to NaN, in which case the date is set to Invalid Date. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setYear(2024);
console.log(someDate);
// Date Thu Jun 06 2024 01:00:00 GMT+0100 (British Summer Time)
console.log(someDate.setYear(NaN));
// NaN
console.log(someDate);
// Invalid Date

If the argument supplied to setYear() is a two-digit number in the range 0 to 99, the number is interpreted as an offset to 1900. Otherwise, the year is set to whatever value is passed as an argument. For example:

const someDate = new Date("2023-06-06");
console.log(someDate);
// Date Tue Jun 06 2023 01:00:00 GMT+0100 (British Summer Time)
someDate.setYear(24);
console.log(someDate);
// Date Fri Jun 06 1924 01:00:00 GMT+0100 (British Summer Time)
someDate.setYear(666);
console.log(someDate);
// Date Wed Jun 06 0666 01:00:00 GMT-0001 (Greenwich Mean Time)

This method is deprecated and should not be used - use setFullYear() instead in new code, and if possible replace setYear() with setFullYear() in existing code.

toDateString()

Syntax:

toDateString()

An instance method that returns a human-readable string representing the date portion of the date represented by the Date object on which it is called, interpreted according to the local time zone, or Invalid Date if the date is invalid. For example:

const someDate = new Date("2023-06-06T11:45:30");
const someOtherDate = new Date("06:06:2023");
console.log(someDate.toDateString());
// Tue Jun 06 2023
console.log(someOtherDate.toDateString());
// Invalid Date

The toDateString() method formats the date string in English, and consists of the following components, separated by spaces:

  • The first three letters of the weekday name
  • The first three letters of the month name
  • A two-digit integer value representing the day of the month, padded with a leading zero if necessary
  • A four-digit (minimum) integer value representing the year, padded with leading zeros if necessary

toISOString()

Syntax:

toISOString()

An instance method that returns a string representation, in a simplified date time string format based on the ISO 8601 standard, of the date represented by the Date object on which it is called according to universal time. For example:

const someDate = new Date("2023-06-06T11:45:30");
console.log(someDate);
// Date Tue Jun 06 2023 11:45:30 GMT+0100 (British Summer Time)
console.log(someDate.toISOString());
// 2023-06-06T10:45:30.000Z

The string returned by toISOString() is always either 24 or 27 characters long, depending on how many digits are used to represent the year (4 or 7) in one of the following formats.

YYYY-MM-DDTHH:mm:ss.sssZ
±YYYYYY-MM-DDTHH:mm:ss.sssZ

If the date is invalid or corresponds to a year that cannot be represented in either of the above formats, a range error is thrown.

toJSON()

Syntax:

toJSON()

An instance method that returns a string representation, in a simplified date time string format based on the ISO 8601 standard, of the date represented by the Date object on which it is called according to universal time, or null if the date is invalid. For example:

const someDate = new Date("2023-06-06T11:45:30");
const someOtherDate = new Date("06:06:2023");
console.log(someDate);
// Date Tue Jun 06 2023 11:45:30 GMT+0100 (British Summer Time)
console.log(someDate.toJSON());
// 2023-06-06T10:45:30.000Z
console.log(someOtherDate.toJSON());
// null

For a valid date, the string returned is identical to the string returned by toISOString().

toLocaleDateString()

Syntax:

toLocaleDateString()
toLocaleDateString(locales)
toLocaleDateString(locales, options)

An instance method that returns a language-sensitive string representing the date portion of the date represented by the Date object on which it is called, interpreted according to the local time zone, or Invalid Date if the date is invalid. For example:

const someDate = new Date("2023-06-06T11:45:30");
const someOtherDate = new Date("06:06:2023");
console.log(someDate);
// Date Tue Jun 06 2023 11:45:30 GMT+0100 (British Summer Time)
console.log(someDate.toLocaleDateString());
// 06/06/2023
console.log(someOtherDate.toLocaleDateString());
// Invalid Date

Note that if the user's browser supports the Intl.DateTimeFormat API, this method simply calls Intl.DateTimeFormat().

The toLocaleDateString() method has two optional parameters that can be used to customise the method's behaviour - locales and options - that correspond to the arguments accepted by the Intl.DateTimeFormat() constructor.

The locales option is a string (officially referred to as a tag) that typically identifies a specific language and region. For example, en-US consists of two sub-tags separated by the "-" character. The first sub-tag identifies the language (en = English), while the second sub-tag identifies the region (US = United States).

Using the locales option will determine how the date is displayed in different languages and regions. For example, US English uses a month-day-year order for dates whereas British English uses a day-month-year order, as the following code demonstrates:

const someDate = new Date("2024-06-30");
console.log(someDate);
// Date Sun Jun 30 2024 01:00:00 GMT+0100 (British Summer Time)
console.log(someDate.toLocaleDateString("en-US"));
// 6/30/2024
console.log(someDate.toLocaleDateString("en-GB"));
// 30/06/2024

The options parameter, as its name suggests, is an object comprising a number of attributes whose values can be set in order to further customise the output of the toLocaleDateString() method. The following example demonstrates how the options parameter might be used:

const someDate = new Date("2024-06-30");
const dateOptions = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
};

console.log(someDate);
// Date Sun Jun 30 2024 01:00:00 GMT+0100 (British Summer Time)
console.log(someDate.toLocaleDateString("en-US", dateOptions));
// Sunday, June 30, 2024
console.log(someDate.toLocaleDateString("en-GB", dateOptions));
// Sunday, 30 June 2024
console.log(someDate.toLocaleDateString("de-DE", dateOptions));
// Sonntag, 30. Juni 2024

Note that the options parameter can only be used in tandem with the locales option. Note also, however, that once created, the same options object can be used in multiple calls to toLocaleDateString(), regardless of the value assigned to the locales option.

If the toLocaleDateString() method is used without any arguments, it returns a date string that is formatted according to the default locale, with the default options for that locale. If the browser implementation does not support the Intl.DateTimeFormat API, it will usually ignore any locale and options arguments supplied, behave as if no arguments were supplied, and use the default locale and options.

toLocaleString()

Syntax:

toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)

An instance method that returns a language-sensitive string representing the date and time represented by the Date object on which it is called, interpreted according to the local time zone, or Invalid Date if the date is invalid. For example:

const someDate = new Date("2023-06-06T11:45:30");
const someOtherDate = new Date("06:06:2023");
console.log(someDate);
// Date Tue Jun 06 2023 11:45:30 GMT+0100 (British Summer Time)
console.log(someDate.toLocaleString());
// 06/06/2023, 11:45:30
console.log(someOtherDate.toLocaleString());
// Invalid Date

Note that if the user's browser supports the Intl.DateTimeFormat API, this method simply calls Intl.DateTimeFormat().

The toLocaleString() method has two optional parameters that can be used to customise the method's behaviour - locales and options - that correspond to the arguments accepted by the Intl.DateTimeFormat() constructor.

The locales option is a string (officially referred to as a tag) that typically identifies a specific language and region. For example, en-US consists of two sub-tags separated by the "-" character. The first sub-tag identifies the language (en = English), while the second sub-tag identifies the region (US = United States).

Using the locales option will determine how the date and time are displayed in different languages and regions. For example, US English uses a month-day-year order for dates and a 12-hour format for time, whereas British English uses a day-month-year order and a 24-hour format for time, as the following code demonstrates:

const someDate = new Date("2024-06-30T15:45:30");
console.log(someDate);
// Date Sun Jun 30 2024 15:45:30 GMT+0100 (British Summer Time)
console.log(someDate.toLocaleString("en-US"));
// 6/30/2024, 3:45:30 PM
console.log(someDate.toLocaleString("en-GB"));
// 30/06/2024, 15:45:30

The options parameter, as its name suggests, is an object comprising a number of attributes whose values can be set in order to further customise the output of the toLocaleString() method. The following example demonstrates how the options parameter might be used:

const someDate = new Date("2024-06-30T09:45:30");
const dateTimeOptions = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric"
};

console.log(someDate);
// Date Sun Jun 30 2024 09:45:30 GMT+0100 (British Summer Time)
console.log(someDate.toLocaleString("en-US", dateTimeOptions));
// Sunday, June 30, 2024 at 9:45:30 AM
console.log(someDate.toLocaleString("en-GB", dateTimeOptions));
// Sunday, 30 June 2024 at 09:45:30
console.log(someDate.toLocaleString("de-DE", dateTimeOptions));
// Sonntag, 30. Juni 2024 um 09:45:30

Note that the options parameter can only be used in tandem with the locales option. Note also, however, that once created, the same options object can be used in multiple calls to toLocaleString(), regardless of the value assigned to the locales option.

If the toLocaleString() method is used without any arguments, it returns a datetime string that is formatted according to the default locale, with the default options for that locale. If the browser implementation does not support the Intl.DateTimeFormat API, it will usually ignore any locale and options arguments supplied, behave as if no arguments were supplied, and use the default locale and options.

toLocaleTimeString()

Syntax:

toLocaleTimeString()
toLocaleTimeString(locales)
toLocaleTimeString(locales, options)

An instance method that returns a language-sensitive string representing the time portion of the date represented by the Date object on which it is called, interpreted according to the local time zone, or Invalid Date if the date is invalid. For example:

const someDate = new Date("2023-06-06T11:45:30");
const someOtherDate = new Date("06.06.2023 11.45.30");
console.log(someDate);
// Date Tue Jun 06 2023 11:45:30 GMT+0100 (British Summer Time)
console.log(someDate.toLocaleTimeString());
// 11:45:30
console.log(someOtherDate.toLocaleTimeString());
// Invalid Date

Note that if the user's browser supports the Intl.DateTimeFormat API, this method simply calls Intl.DateTimeFormat().

The toLocaleTimeString() method has two optional parameters that can be used to customise the method's behaviour - locales and options - that correspond to the arguments accepted by the Intl.DateTimeFormat() constructor.

The locales option is a string (officially referred to as a tag) that typically identifies a specific language and region. For example, en-US consists of two sub-tags separated by the "-" character. The first sub-tag identifies the language (en = English), while the second sub-tag identifies the region (US = United States).

Using the locales option will determine how the time is displayed in different languages and regions. For example, US English uses a 12-hour format for time, whereas British English uses a 24-hour format for time, as the following code demonstrates:

const someDate = new Date("2024-06-30T15:45:30");
console.log(someDate);
// Date Sun Jun 30 2024 15:45:30 GMT+0100 (British Summer Time)
console.log(someDate.toLocaleTimeString("en-US"));
// 3:45:30 PM
console.log(someDate.toLocaleTimeString("en-GB"));
// 15:45:30

The options parameter, as its name suggests, is an object comprising a number of attributes whose values can be set in order to further customise the output of the toLocaleTimeString() method. The following example demonstrates how the options parameter might be used:

const someDate = new Date("2024-06-30T06:30:45");
const timeOptions = {
hour12: true,
dayPeriod: "long",
hour: "numeric",
minute: "numeric",
second: "numeric"
};

console.log(someDate);
// Date Sun Jun 30 2024 06:30:45 GMT+0100 (British Summer Time)
console.log(someDate.toLocaleTimeString("en-GB", timeOptions));
// 6:30:45 in the morning
console.log(someDate.toLocaleTimeString("de-DE", timeOptions));
// 6:30:45 morgens
console.log(someDate.toLocaleTimeString("el-GR", timeOptions));
// 6:30:45 το πρωί

Note that the options parameter can only be used in tandem with the locales option. Note also, however, that once created, the same options object can be used in multiple calls to toLocaleTimeString(), regardless of the value assigned to the locales option.

If the toLocaleTimeString() method is used without any arguments, it returns a time string that is formatted according to the default locale, with the default options for that locale. If the browser implementation does not support the Intl.DateTimeFormat API, it will usually ignore any locale and options arguments supplied, behave as if no arguments were supplied, and use the default locale and options.

toString()

Syntax:

toString()

An instance method that returns a string representation of the date represented by the Date object on which it is called according to local time, or Invalid Date if the date is invalid. For example:

const someDate = new Date("2023-06-06T11:45:30");
const someOtherDate = new Date("06.06.2023 11.45.30");
console.log(someDate);
// Date Tue Jun 06 2023 11:45:30 GMT+0100 (British Summer Time)
console.log(someDate.toString());
// Tue Jun 06 2023 11:45:30 GMT+0100 (British Summer Time)
console.log(someOtherDate.toString());
// Invalid Date

The toString() method returns the string that results from joining the string that would be returned by returned by toDateString() with the string that would be returned by toTimeString(), separated by a space.

toTimeString()

Syntax:

toTimeString()

An instance method that returns a human-readable string representing the time portion of the date represented by the Date object on which it is called, interpreted according to the local time zone, or Invalid Date if the date is invalid. For example:

const someDate = new Date("2023-06-06T11:45:30");
const someOtherDate = new Date("06:06:2023 11:45:30"); console.log(someDate.toTimeString());
// 11:45:30 GMT+0100 (British Summer Time)
console.log(someOtherDate.toTimeString());
// Invalid Date

The toTimeString() method formats the time string in English using the format hh:mm:ss GMT+xxxx (TZ) where:

    hh = the hours as two-digits, with a leading zero if required
    mm = the minutes as two-digits, with a leading zero if required
    ss = the seconds as two-digits, with a leading zero if required
    ±xxxx = the local timezone offset (two digits each for hours and minutes)
    TZ = the name of the time zone

toUTCString()

Syntax:

toUTCString()

An instance method that returns a string representing the date represented by the Date object on which it is called, interpreted according to universal time, or Invalid Date if the date is invalid. For example:

const someDate = new Date("2023-06-06T11:45:30");
const someOtherDate = new Date("06:06:2023 11:45:30");
console.log(someDate.toUTCString());
// Tue, 06 Jun 2023 10:45:30 GMT
console.log(someOtherDate.toUTCString());
// Invalid Date

The toUTCString() method formats the date string using the format Www, dd Mmm yyyy hh:mm:ss GMT where:

    Www = the day of the week as three letters (e.g. Sun, Mon)
    dd = the day of the month as two digits, with a leading zero if required
    Mmm = the month as three letters (e.g. Jan, Feb)
    yyyy = the year as four or more digits, with leading zeros if required
    hh = the hour as two digits, with a leading zero if required
    mm = the minute as two digits, with a leading zero if required
    ss = the seconds as two digits, with a leading zero if required

valueOf()

Syntax:

valueOf()

An instance method, functionally equivalent to the getTime() method, that returns an integer value representing the number of milliseconds that have elapsed between the epoch (00:00:00 UTC on January 1st, 1970) and the date and time specified by the Date object on which it is called, or NaN. Otherwise known as a timestamp. For example:

const xmas2024 = new Date("2024-12-25");
console.log(xmas2024.valueOf());
// 1735084800000

Returns NaN if the date is invalid.