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
Method | Description |
---|---|
Date() |
Syntax:
new 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(); 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"); 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)); 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 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()); 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(); |
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"); If Date.parse() receives an invalid date string, it will return NaN. For example:
const someDate = Date.parse("2nd March, 2025"); 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"); 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) 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); 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 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); 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"); 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"]; 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"); 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"); 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"); 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"); 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"]; 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"); 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"); 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"); 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"); 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"); 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"]; 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"); 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"); 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"); 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"); 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"]; 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"); 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"); 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"); 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"); 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"); |
setFullYear() |
Syntax:
setFullYear(yearValue) 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"); 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"); 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"); 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"); 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"); 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"); |
setHours() |
Syntax:
setHours(hoursValue)
setHours(hoursValue, minutesValue) 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"); 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"); 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"); 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"); 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"); 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"); 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"); 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"); 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"); |
setMinutes() |
Syntax:
setMinutes(minutesValue) 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"); 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"); 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"); 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"); 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"); 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) 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"); 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"); 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"); 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"); 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"); 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"); 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) 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"); 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"); 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"); 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"); 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"); 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"); 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"); 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"); 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"); |
setUTCFullYear() |
Syntax:
setUTCFullYear(yearValue) 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"); 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"); 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"); 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"); 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"); 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"); |
setUTCHours() |
Syntax:
setUTCHours(hoursValue) 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"); 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"); 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"); 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"); 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"); 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"); 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"); 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"); 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"); |
setUTCMinutes() |
Syntax:
setUTCMinutes(minutesValue) 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"); 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"); 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"); 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"); 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"); 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) 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"); 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"); 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"); 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"); 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"); 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"); 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) 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"); 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"); 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"); 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"); 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"); 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"); 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"); The toDateString() method formats the date string in English, and consists of the following components, separated by spaces:
|
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"); 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 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"); For a valid date, the string returned is identical to the string returned by toISOString(). |
toLocaleDateString() |
Syntax:
toLocaleDateString() 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"); 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"); 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"); 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() 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"); 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"); 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"); 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() 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"); 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"); 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"); 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"); 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"); 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 |
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"); 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) |
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"); Returns NaN if the date is invalid. |