Date API Flashcards
Date()
constructor
The Date()
constructor creates Date
objects. When called as a function, it returns a string representing the current time.
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()
Note: Date()
can be called with or without new, but with different effects.
Parameters
There are five basic forms for the Date()
constructor:
No parameters
When no parameters are provided, the newly-created Date
object represents the current date and time as of the time of instantiation. The returned date’s timestamp is the same as the number returned by Date.now()
.
Time value or timestamp number
-
value
- An integer value representing the timestamp (the number of milliseconds since midnight at the beginning of January 1, 1970, UTC — a.k.a. the epoch).
Date string
-
dateString
- A string value representing a date, parsed and interpreted using the same algorithm implemented by Date.parse(). See date time string format for caveats on using different formats.
Date object
-
dateObject
- An existingDate
object. This effectively makes a copy of the existingDate
object with the same date and time. This is equivalent tonew Date(dateObject.valueOf())
, except thevalueOf()
method is not called.
When one parameter is passed to the Date()
constructor, Date
instances are specially treated. All other values are converted to primitives. If the result is a string
, it will be parsed as a date string. Otherwise, the resulting primitive is further coerced to a number
and treated as a timestamp.
Individual date and time component values
Given at least a year
and month
, this form of Date()
returns a Date
object whose component values (year
, month
, day
, hour
, minute
, second
, and millisecond
) all come from the following parameters. Any missing fields are given the lowest possible value (1
for day and 0
for every other component). The parameter values are all evaluated against the local time zone, rather than UTC. Date.UTC()
accepts similar parameters but interprets the components as UTC and returns a timestamp.
If any parameter overflows its defined bounds, it “carries over”. For example, if a monthIndex
greater than 11
is passed in, those months will cause the year to increment; if a minutes
greater than 59
is passed in, hours will increment accordingly, etc. Therefore, new Date(1990, 12, 1)
will return January 1st, 1991
; new Date(2020, 5, 19, 25, 65)
will return 2:05 A.M. June 20th, 2020
.
Similarly, if any parameter underflows, it “borrows” from the higher positions. For example, new Date(2020, 5, 0)
will return May 31st, 2020
.
-
year
- Integer value representing the year. Values from0
to99
map to the years1900
to1999
. All other values are the actual year. See the example. -
monthIndex
- Integer value representing the month, beginning with0
for January to11
for December. -
day
Optional - Integer value representing the day of the month. Defaults to1
. -
hours
Optional - Integer value between0
and23
representing the hour of the day. Defaults to0
. -
minutes
Optional - Integer value representing the minute segment of a time. Defaults to0
. -
seconds
Optional - Integer value representing the second segment of a time. Defaults to0
. -
milliseconds
Optional - Integer value representing the millisecond segment of a time. Defaults to0
.
Return value
Calling new Date()
(the Date()
constructor) returns a Date
object. If called with an invalid date string, or if the date to be constructed will have a timestamp less than -8,640,000,000,000,000
or greater than 8,640,000,000,000,000
milliseconds, it returns an invalid date (a Date
object whose toString()
method returns "Invalid Date"
and valueOf()
method returns NaN
).
Calling the Date()
function (without the new
keyword) returns a string representation of the current date and time, exactly as new Date().toString()
does. Any arguments given in a Date()
function call (without the new
keyword) are ignored; regardless of whether it’s called with an invalid date string — or even called with any arbitrary object or other primitive as an argument — it always returns a string representation of the current date and time.
“Date() constructor - JavaScript | MDN” (MDN Web Docs). Retrieved March 14, 2024.
Date.prototype[@@toPrimitive]()
The [@@toPrimitive]()
method of Date
instances returns a primitive value representing this date. It may either be a string or a number, depending on the hint given.
The [@@toPrimitive]()
method is part of the type coercion protocol. JavaScript always calls the [@@toPrimitive]()
method in priority to convert an object to a primitive value. You rarely need to invoke the [@@toPrimitive]()
method yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected.
Syntax
date[Symbol.toPrimitive](hint)
Parameters
hint
- A string representing the type of the primitive value to return. The following values are valid:
-
"string"
or"default"
: The method should return astring
. -
"number"
: The method should return anumber
.
Return value
If hint is "string"
or "default"
, this method returns a string by coercing the this value to a string (first trying toString()
then trying valueOf()
).
If hint is "number"
, this method returns a number by coercing the this value to a number
(first trying valueOf()
then trying toString()
).
Exceptions
TypeError
- Thrown if the hint argument is not one of the three valid values.
Examples:
const d = new Date(0); // 1970-01-01T00:00:00.000Z d[Symbol.toPrimitive]("string"); // "Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)" d[Symbol.toPrimitive]("number"); // 0 d[Symbol.toPrimitive]("default"); // "Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)"
“Date.prototype[@@toPrimitive]() - JavaScript | MDN” (MDN Web Docs). Retrieved March 20, 2024.
Date.prototype.getDate()
The getDate()
method of Date
instances returns the day of the month for this date according to local time.
Syntax
getDate()
Parameters
None.
Return value
An integer, between 1
and 31
, representing the day of the month for the given date according to local time. Returns NaN
if the date is invalid.
Examples:
const xmas95 = new Date("1995-12-25T23:15:30"); const day = xmas95.getDate(); console.log(day); // 25
“Date.prototype.getDate() - JavaScript | MDN” (MDN Web Docs). Retrieved March 20, 2024.
Date.prototype.getDay()
The getDay()
method of Date
instances returns the day of the week for this date according to local time, where 0
represents Sunday. For the day of the month, see Date.prototype.getDate()
.
Syntax
getDay()
Parameters
None.
Return value
An integer, between 0
and 6
, representing the day of the week for the given date according to local time: 0
for Sunday, 1
for Monday, 2
for Tuesday, and so on. Returns NaN
if the date is invalid.
Description
The return value of getDay()
is zero-based, which is useful for indexing into arrays of days, for example:
const valentines = new Date("1995-02-14"); const day = valentines.getDay(); const dayNames = ["Sunday", "Monday", "Tuesday" /* , … */]; console.log(dayNames[day]); // "Monday"
However, for the purpose of internationalization, you should prefer using Intl.DateTimeFormat
with the options parameter instead.
const options = { weekday: "long" }; console.log(new Intl.DateTimeFormat("en-US", options).format(valentines)); // "Monday" console.log(new Intl.DateTimeFormat("de-DE", options).format(valentines)); // "Montag"
Examples:
const xmas95 = new Date("1995-12-25T23:15:30"); const weekday = xmas95.getDay(); console.log(weekday); // 1
“Date.prototype.getDay() - JavaScript | MDN” (MDN Web Docs). Retrieved March 20, 2024.
Date.prototype.getFullYear()
The getFullYear()
method of Date
instances returns the year for this date according to local time.
Unlike getYear()
, the value returned by getFullYear()
is an absolute number. For dates between the years 1000
and 9999
, getFullYear()
returns a four-digit number, for example, 1995
. Use this function to make sure a year is compliant with years after 2000
.
Syntax
getFullYear()
Parameters
None.
Return value
An integer representing the year for the given date according to local time. Returns NaN
if the date is invalid.
Examples:
const xmas95 = new Date("1995-12-25T23:15:30"); const fullYear = xmas95.getFullYear(); console.log(fullYear); // 1995
“Date.prototype.getFullYear() - JavaScript | MDN” (MDN Web Docs). Retrieved March 20, 2024.
Date.prototype.getHours()
The getHours()
method of Date
instances returns the hours for this date according to local time.
Syntax
getHours()
Parameters
None.
Return value
An integer, between 0
and 23
, representing the hours for the given date according to local time. Returns NaN
if the date is invalid.
Examples:
const xmas95 = new Date("1995-12-25T23:15:30"); const hours = xmas95.getHours(); console.log(hours); // 23
“Date.prototype.getHours() - JavaScript | MDN” (MDN Web Docs). Retrieved March 21, 2024.
Date.prototype.getMilliseconds()
The getMilliseconds()
method of Date
instances returns the milliseconds for this date according to local time.
Syntax
getMilliseconds()
Parameters
None.
Return value
An integer, between 0
and 999
, representing the milliseconds for the given date according to local time. Returns NaN
if the date is invalid.
Examples:
const xmas95 = new Date("1995-12-25T23:15:30"); const milliseconds = xmas95.getMilliseconds(); console.log(milliseconds); // 0
“Date.prototype.getMilliseconds() - JavaScript | MDN” (MDN Web Docs). Retrieved March 21, 2024.
Date.prototype.getMinutes()
The getMinutes()
method of Date
instances returns the minutes for this date according to local time.
Syntax
getMinutes()
Parameters
None.
Return value
An integer, between 0
and 59
, representing the minutes for the given date according to local time. Returns NaN
if the date is invalid.
Examples:
const xmas95 = new Date("1995-12-25T23:15:30"); const minutes = xmas95.getMinutes(); console.log(minutes); // 15
“Date.prototype.getMinutes() - JavaScript | MDN” (MDN Web Docs). Retrieved March 21, 2024.
Date.prototype.getMonth()
The getMonth()
method of Date
instances returns the month for this date according to local time, as a zero-based value (where zero indicates the first month of the year).
The return value of getMonth()
is zero-based, which is useful for indexing into arrays of months, for example:
Syntax
getMonth()
Parameters
None.
Return value
An integer, between 0
and 11
, representing the month for the given date according to local time: 0
for January
, 1
for February
, and so on. Returns NaN
if the date is invalid.
Examples:
const valentines = new Date("1995-02-14"); const month = valentines.getMonth(); const monthNames = ["January", "February", "March" /* , … */]; console.log(monthNames[month]); // "February"
“Date.prototype.getMonth() - JavaScript | MDN” (MDN Web Docs). Retrieved March 21, 2024.
Date.prototype.getSeconds()
The getSeconds()
method of Date
instances returns the seconds for this date according to local time.
Syntax
getSeconds()
Parameters
None.
Return value
An integer, between 0
and 59
, representing the seconds for the given date according to local time. Returns NaN
if the date is invalid.
Examples:
const xmas95 = new Date("1995-12-25T23:15:30"); const seconds = xmas95.getSeconds(); console.log(seconds); // 30
“Date.prototype.getSeconds() - JavaScript | MDN” (MDN Web Docs). Retrieved March 21, 2024.
Date.prototype.getTime()
The getTime()
method of Date
instances returns the number of milliseconds for this date since the epoch
, which is defined as the midnight at the beginning of January 1, 1970, UTC
.
Date
objects are fundamentally represented by a timestamp, and this method allows you to retrieve the timestamp. You can use this method to help assign a date and time to another Date
object. This method is functionally equivalent to the valueOf()
method.
Reduced time precision
To offer protection against timing attacks and fingerprinting, the precision of new Date().getTime()
might get rounded depending on browser settings. In Firefox, the privacy.reduceTimerPrecision
preference is enabled by default and defaults to 2ms
. You can also enable privacy.resistFingerprinting
, in which case the precision will be 100ms
or the value of privacy.resistFingerprinting
, reduceTimerPrecision.microseconds,
whichever is larger.
// reduced time precision (2ms) in Firefox 60 new Date().getTime(); // 1519211809934 // 1519211810362 // 1519211811670 // … // reduced time precision with `privacy.resistFingerprinting` enabled new Date().getTime(); // 1519129853500 // 1519129858900 // 1519129864400 // …
Syntax
getTime()
Parameters
None.
Return value
A number representing the timestamp, in milliseconds, of this date. Returns NaN
if the date is invalid.
Examples:
// Since month is zero based, birthday will be January 10, 1995 const birthday = new Date(1994, 12, 10); const copy = new Date(); copy.setTime(birthday.getTime());
Note: In browsers that support the Web Performance API’s high-resolution time feature, Performance.now()
can provide more reliable and precise measurements of elapsed time than Date.now()
.
“Date.prototype.getTime() - JavaScript | MDN” (MDN Web Docs). Retrieved March 21, 2024.
Date.prototype.getTimezoneOffset()
The getTimezoneOffset()
method of Date
instances returns the difference, in minutes, between this date as evaluated in the UTC time zone, and the same date as evaluated in the local time zone.
Negative values and positive values
The number of minutes returned by getTimezoneOffset()
is positive if the local time zone is behind UTC, and negative if the local time zone is ahead of UTC. For example, for UTC+10
, -600
will be returned.
Syntax
getTimezoneOffset()
Parameters
None.
Return value
A number representing the difference, in minutes, between the date as evaluated in the UTC time zone and as evaluated in the local time zone. The actual local time algorithm is implementation-defined, and the return value is allowed to be zero in runtimes without appropriate data. Returns NaN
if the date is invalid.
Examples:
// Create a Date instance for the current time const currentLocalDate = new Date(); // Create a Date instance for 03:24 GMT-0200 on May 1st in 2016 const laborDay2016at0324GMTminus2 = new Date("2016-05-01T03:24:00-02:00"); currentLocalDate.getTimezoneOffset() === laborDay2016at0324GMTminus2.getTimezoneOffset(); // true, always, in any timezone that doesn't annually shift in and out of DST // false, sometimes, in any timezone that annually shifts in and out of DST
“Date.prototype.getTimezoneOffset() - JavaScript | MDN” (MDN Web Docs). Retrieved March 22, 2024.
Date.prototype.getUTCDate()
The getUTCDate()
method of Date
instances returns the day of the month for this date according to universal time.
Syntax
getUTCDate()
Parameters
None.
Return value
An integer, between 1
and 31
, representing day of month for the given date according to universal time. Returns NaN
if the date is invalid.
Examples:
const today = new Date(); const dayOfMonth = today.getUTCDate();
“Date.prototype.getUTCDate() - JavaScript | MDN” (MDN Web Docs). Retrieved March 22, 2024.
Date.prototype.getUTCDay()
The getUTCDay()
method of Date instances returns the day of the week for this date according to universal time, where 0
represents Sunday
.
Syntax
getUTCDay()
Parameters
None.
Return value
An integer corresponding to the day of the week for the given date according to universal time: 0
for Sunday
, 1
for Monday
, 2
for Tuesday
, and so on. Returns NaN
if the date is invalid.
Examples:
const today = new Date(); const weekday = today.getUTCDay();
“Date.prototype.getUTCDay() - JavaScript | MDN” (MDN Web Docs). Retrieved March 22, 2024.
Date.prototype.getUTCFullYear()
The getUTCFullYear()
method of Date
instances returns the year for this date according to universal time.
Unlike getYear()
, the value returned by getUTCFullYear()
is an absolute number. For dates between the years 1000
and 9999
, getUTCFullYear()
returns a four-digit number, for example, 1995
. Use this function to make sure a year is compliant with years after 2000
.
Syntax
getUTCFullYear()
Parameters
None.
Return value
An integer representing the year for the given date according to universal time. Returns NaN
if the date is invalid.
Examples:
const today = new Date(); const year = today.getUTCFullYear();
“Date.prototype.getUTCFullYear() - JavaScript | MDN” (MDN Web Docs). Retrieved March 22, 2024.
Date.prototype.getUTCHours()
The getUTCHours()
method of Date
instances returns the hours for this date according to universal time.
Syntax
getUTCHours()
Parameters
None.
Return value
An integer, between 0
and 23
, representing the hours for the given date according to universal time. Returns NaN
if the date is invalid.
Examples:
const today = new Date(); const hours = today.getUTCHours();
“Date.prototype.getUTCHours() - JavaScript | MDN” (MDN Web Docs). Retrieved March 25, 2024.
Date.prototype.getUTCMilliseconds()
The getUTCMilliseconds()
method of Date
instances returns the milliseconds for this date according to universal time.
Syntax
getUTCMilliseconds()
Parameters
None.
Return value
An integer, between 0
and 999
, representing the milliseconds for the given date according to universal time. Returns NaN
if the date is invalid.
Not to be confused with the timestamp. To get the total milliseconds since the epoch, use the getTime()
method.
Examples:
const today = new Date(); const milliseconds = today.getUTCMilliseconds();
“Date.prototype.getUTCMilliseconds() - JavaScript | MDN” (MDN Web Docs). Retrieved March 25, 2024.
Date.prototype.getUTCMinutes()
The getUTCMinutes()
method of Date
instances returns the minutes for this date according to universal time.
Syntax
getUTCMinutes()
Parameters
None.
Return value
An integer, between 0
and 59
, representing the minutes for the given date according to universal time. Returns NaN
if the date is invalid.
Examples
const today = new Date(); const minutes = today.getUTCMinutes();
“Date.prototype.getUTCMinutes() - JavaScript | MDN” (MDN Web Docs). Retrieved March 25, 2024.
Date.prototype.getUTCMonth()
The getUTCMonth()
method of Date
instances returns the month for this date according to universal time, as a zero-based value (where zero indicates the first month of the year).
Syntax
getUTCMonth()
Parameters
None.
Return value
An integer, between 0
and 11
, representing the month for the given date according to universal time: 0
for January
, 1
for February
, and so on. Returns NaN
if the date is invalid.
Examples:
const today = new Date(); const month = today.getUTCMonth();
“Date.prototype.getUTCMonth() - JavaScript | MDN” (MDN Web Docs). Retrieved March 25, 2024.
Date.prototype.getUTCSeconds()
The getUTCSeconds()
method of Date
instances returns the seconds in the specified date according to universal time.
Syntax
getUTCSeconds()
Parameters
None.
Return value
An integer, between 0
and 59
, representing the seconds for the given date according to universal time. Returns NaN
if the date is invalid.
Examples:
const today = new Date(); const seconds = today.getUTCSeconds();
“Date.prototype.getUTCSeconds() - JavaScript | MDN” (MDN Web Docs). Retrieved March 25, 2024.
Date.prototype.getYear()
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards.
The getYear()
method of Date
instances returns the year for this date according to local time. Because getYear()
does not return full years (“year 2000 problem”), it is deprecated and has been replaced by the getFullYear()
method.
Syntax
getYear()
Parameters
None.
Return value
An integer representing the year for the given date according to local time, minus 1900
. Returns NaN
if the date is invalid.
- For years greater than or equal to
2000
, the value is100
or greater. For example, if the year is2026
,getYear()
returns126
. - For years between and including
1900
and1999
, the value returned bygetYear()
is between0
and99
. For example, if the year is1976
,getYear()
returns76
. - For years less than
1900
, the value returned bygetYear()
is less than ç. For example, if the year is1800
,getYear()
returns-100
.
This method essentially returns the value ofgetFullYear()
minus1900
. You should usegetFullYear()
instead, so that the year is specified in full.
Examples:
// Years between 1900 and 1999 let xmas = new Date("1995-12-25"); let year = xmas.getYear(); // returns 95 // Years above 1999 xmas = new Date("2000-12-25"); year = xmas.getYear(); // returns 100 // Years below 1900 xmas = new Date("1800-12-25"); year = xmas.getYear(); // returns -100
“Date.prototype.getYear() - JavaScript | MDN” (MDN Web Docs). Retrieved March 25, 2024.
Date.now()
The Date.now()
static method returns the number of milliseconds elapsed since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC
.
Reduced time precision
To offer protection against timing attacks and fingerprinting, the precision of Date.now()
might get rounded depending on browser settings.
Syntax
Date.now()
Parameters
None.
Return value
A number representing the timestamp, in milliseconds, of the current time.
“Date.now() - JavaScript | MDN” (MDN Web Docs). Retrieved March 26, 2024.
Date.parse()
The Date.parse()
static method parses a string representation of a date, and returns the date’s timestamp.
Syntax
Date.parse(dateString)
Parameters
-
dateString
- A string in the date time string format.
Return value
A number representing the timestamp of the given date. If dateString fails to be parsed as a valid date, NaN
is returned.
Examples:
The following calls all return 1546300800000. The first will imply UTC time because it’s date-only, and the others explicitly specify the UTC timezone.
Date.parse("2019-01-01"); Date.parse("2019-01-01T00:00:00.000Z"); Date.parse("2019-01-01T00:00:00.000+00:00");
“Date.parse()
- JavaScript | MDN” (MDN Web Docs). Retrieved March 26, 2024.
Date.prototype.setDate()
The setDate()
method of Date
instances changes the day of the month for this date according to local time.
Syntax
setDate(dateValue)
Parameters
-
dateValue
- An integer representing the day of the month.
Return value
Changes the Date object in place, and returns its new timestamp. If dateValue
is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date and NaN
is returned.
Examples:
const theBigDay = new Date(1962, 6, 7, 12); // noon of 1962-07-07 (7th of July 1962, month is 0-indexed) const theBigDay2 = new Date(theBigDay).setDate(24); // 1962-07-24 (24th of July 1962) const theBigDay3 = new Date(theBigDay).setDate(32); // 1962-08-01 (1st of August 1962) const theBigDay4 = new Date(theBigDay).setDate(22); // 1962-07-22 (22nd of July 1962) const theBigDay5 = new Date(theBigDay).setDate(0); // 1962-06-30 (30th of June 1962) const theBigDay6 = new Date(theBigDay).setDate(98); // 1962-10-06 (6th of October 1962) const theBigDay7 = new Date(theBigDay).setDate(-50); // 1962-05-11 (11th of May 1962)
“Date.prototype.setDate() - JavaScript | MDN” (MDN Web Docs). Retrieved March 26, 2024.
Date.prototype.setFullYear()
The setFullYear()
method of Date
instances changes the year, month, and/or day of month for this date according to local time.
Syntax
setFullYear(yearValue) setFullYear(yearValue, monthValue) setFullYear(yearValue, monthValue, dateValue)
Parameters
-
yearValue
- An integer representing the year. For example,1995
. -
monthValue
Optional - An integer representing the month:0
forJanuary
,1
forFebruary
, and so on. -
dateValue
Optional - An integer between1
and31
representing the day of the month. If you specify dateValue, you must also specify monthValue.
Return value
Changes the Date
object in place, and returns its new timestamp. If a parameter is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date and NaN
is returned.
“Date.prototype.setFullYear() - JavaScript | MDN” (MDN Web Docs). Retrieved March 26, 2024.
Date.prototype.setHours()
The setHours()
method of Date
instances changes the hours, minutes, seconds, and/or milliseconds for this date according to local time.
Syntax
setHours(hoursValue) setHours(hoursValue, minutesValue) setHours(hoursValue, minutesValue, secondsValue) setHours(hoursValue, minutesValue, secondsValue, msValue)
Parameters
-
hoursValue
- An integer between0
and23
representing the hours. -
minutesValue
Optional - An integer between0
and59
representing the minutes. -
secondsValue
Optional - An integer between0
and59
representing the seconds. If you specifysecondsValue
, you must also specifyminutesValue
. -
msValue
Optional - An integer between0
and999
representing the milliseconds. If you specifymsValue
, you must also specifyminutesValue
andsecondsValue
.
Description
If you do not specify the minutesValue
, secondsValue
, and
msValue parameters, the same values as what are returned by getMinutes()
, getSeconds()
, and getMilliseconds()
are used.
If a parameter you specify is outside of the expected range, other parameters and the date information in the Date
object are updated accordingly. For example, if you specify 100
for secondsValue, the minutes are incremented by 1
(minutesValue + 1
), and 40
is used for seconds.
Return value
Changes the Date
object in place, and returns its new timestamp. If a parameter is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date and NaN
is returned.
Examples:
const theBigDay = new Date(); theBigDay.setHours(7);
“Date.prototype.setHours() - JavaScript | MDN” (MDN Web Docs). Retrieved March 27, 2024.
Date.prototype.setMilliseconds()
The setMilliseconds()
method of Date
instances changes the milliseconds for this date according to local time.
Syntax
setMilliseconds(millisecondsValue)
Parameters
-
millisecondsValue
- An integer between0
and999
representing the milliseconds.
Return value
Changes the Date
object in place, and returns its new timestamp. If millisecondsValue is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date and NaN
is returned.
Description
If you specify a number outside the expected range, the date information in the Date
object is updated accordingly. For example, if you specify 1005
, the number of seconds is incremented by 1
, and 5
is used for the milliseconds.
Examples:
const theBigDay = new Date(); theBigDay.setMilliseconds(100);
“Date.prototype.setMilliseconds() - JavaScript | MDN” (MDN Web Docs). Retrieved March 27, 2024.
Date.prototype.setMinutes()
The setMinutes()
method of Date
instances changes the minutes for this date according to local time.
Syntax
setMinutes(minutesValue) setMinutes(minutesValue, secondsValue) setMinutes(minutesValue, secondsValue, msValue)
Parameters
-
minutesValue
- An integer between0
and59
representing the minutes. -
secondsValue
Optional - An integer between0
and59
representing the seconds. If you specifysecondsValue
, you must also specifyminutesValue
. -
msValue
Optional - An integer between0
and999
representing the milliseconds. If you specifymsValue
, you must also specifyminutesValue
andsecondsValue
.
Return value
Changes the Date
object in place, and returns its new timestamp. If a parameter is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date and NaN
is returned.
Description
If you do not specify the secondsValue and msValue
parameters, the same values as what are returned by getSeconds()
and getMilliseconds()
are used.
If a parameter you specify is outside of the expected range, other parameters and the date information in the Date
object are updated accordingly. For example, if you specify 100
for secondsValue
, the minutes is incremented by 1
(minutesValue + 1
), and 40
is used for seconds.
Examples:
const theBigDay = new Date(); theBigDay.setMinutes(45);
“Date.prototype.setMinutes() - JavaScript | MDN” (MDN Web Docs). Retrieved March 27, 2024.
Date.prototype.setMonth()
The setMonth()
method of Date
instances changes the month and/or day of the month for this date according to local time.
Syntax
setMonth(monthValue) setMonth(monthValue, dateValue)
Parameters
-
monthValue
- An integer representing the month:0
for January,1
for February, and so on. -
dateValue
Optional - An integer from1
to31
representing the day of the month.
Return value
Changes the Date
object in place, and returns its new timestamp. If a parameter is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date and NaN
is returned.
Description
If you do not specify the dateValue
parameter, the same value as what is returned by getDate()
is used.
If a parameter you specify is outside of the expected range, other parameters and the date information in the Date
object are updated accordingly. For example, if you specify 15
for monthValue
, the year is incremented by 1
, and 3
is used for month.
The current day of month will have an impact on the behavior of this method. Conceptually it will add the number of days given by the current day of the month to the 1st day of the new month specified as the parameter, to return the new date. For example, if the current value is 31st January 2016
, calling setMonth
with a value of 1
will return 2nd March 2016
. This is because in 2016 February had 29 days.
Examples:
theBigDay.setMonth(6); //Watch out for end of month transitions const endOfMonth = new Date(2016, 7, 31); endOfMonth.setMonth(1); console.log(endOfMonth); //Wed Mar 02 2016 00:00:00
“Date.prototype.setMonth() - JavaScript | MDN” (MDN Web Docs). Retrieved March 27, 2024.
Date.prototype.setSeconds()
The setSeconds()
method of Date
instances changes the seconds and/or milliseconds for this date according to local time.
Syntax
setSeconds(secondsValue) setSeconds(secondsValue, msValue)
Parameters
-
secondsValue
- An integer between0
and59
representing the seconds. -
msValue
Optional - An integer between0
and999
representing the milliseconds.
Return value
Changes the Date
object in place, and returns its new timestamp. If a parameter is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date
and NaN
is returned.
Description
If you do not specify the msValue
parameter, the value returned from the getMilliseconds()
method is used.
If a parameter you specify is outside of the expected range, setSeconds()
attempts to update the date information in the Date
object accordingly. For example, if you use 100
for secondsValue
, the minutes stored in the Date
object will be incremented by 1
, and 40
will be used for seconds.
Examples:
const theBigDay = new Date(); theBigDay.setSeconds(30);
“Date.prototype.setSeconds() - JavaScript | MDN” (MDN Web Docs). Retrieved March 28, 2024.
Date.prototype.setTime()
The setTime()
method of Date
instances changes the timestamp for this date, which is the number of milliseconds since the epoch, defined as the midnight at the beginning of January 1, 1970, UTC.
Syntax
setTime(timeValue)
Parameters
-
timeValue
- An integer representing the new timestamp — the number of milliseconds since the midnight at the beginning of January 1, 1970, UTC.
Return value
Changes the Date
object in place, and returns its new timestamp. If timeValue is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date and NaN
is returned.
Examples:
const theBigDay = new Date("1999-07-01"); const sameAsBigDay = new Date(); sameAsBigDay.setTime(theBigDay.getTime());
“Date.prototype.setTime() - JavaScript | MDN” (MDN Web Docs). Retrieved March 28, 2024.
Date.prototype.setUTCDate()
The setUTCDate()
method of Date
instances changes the day of the month for this date according to universal time.
Syntax
setUTCDate(dateValue)
Parameters
-
dateValue
- An integer from1
to31
representing the day of the month.
Return value
Changes the Date
object in place, and returns its new timestamp. If dateValue is NaN
(or other values that get coerced to NaN
, such as undefined), the date is set to Invalid Date and NaN
is returned.
Description
If the dateValue
is outside of the range of date values for the month, setDate()
will update the Date
object accordingly.
For example, if 0
is provided for dateValue, the date will be set to the last day of the previous month. If you use 40
for dateValue, and the month stored in the Date
object is June
, the day will be changed to 10
and the month will be incremented to July
.
If a negative number is provided for dateValue
, the date will be set counting backwards from the last day of the previous month. -1
would result in the date being set to 1
day before the last day of the previous month.
Examples:
const theBigDay = new Date(); theBigDay.setUTCDate(20);
“Date.prototype.setUTCDate() - JavaScript | MDN” (MDN Web Docs). Retrieved March 28, 2024.
Date.prototype.setUTCFullYear()
The setUTCFullYear()
method of Date
instances changes the year for this date according to universal time.
Syntax
setUTCFullYear(yearValue) setUTCFullYear(yearValue, monthValue) setUTCFullYear(yearValue, monthValue, dateValue)
Parameters
-
yearValue
- An integer representing the year. For example,1995
. -
monthValue
Optional - An integer representing the month:0
forJanuary
,1
forFebruary
, and so on. -
dateValue
Optional - An integer between1
and31
representing the day of the month. If you specifydateValue
, you must also specifymonthValue
.
Return value
Changes the Date
object in place, and returns its new timestamp. If a parameter is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date and NaN
is returned.
Description
If you do not specify the monthValue
and dateValue
parameters, the values returned from the getUTCMonth()
and getUTCDate()
methods are used.
If a parameter you specify is outside of the expected range, setUTCFullYear()
attempts to update the other parameters and the date information in the Date object accordingly. For example, if you specify 15
for monthValue
, the year is incremented by 1
(yearValue + 1
), and 3
is used for the month.
Examples
const theBigDay = new Date(); theBigDay.setUTCFullYear(1997);
“Date.prototype.setUTCFullYear() - JavaScript | MDN” (MDN Web Docs). Retrieved March 28, 2024.
Date.prototype.setUTCHours()
The setUTCHours()
method of Date
instances changes the hours, minutes, seconds, and/or milliseconds for this date according to universal time.
Syntax
setUTCHours(hoursValue) setUTCHours(hoursValue, minutesValue) setUTCHours(hoursValue, minutesValue, secondsValue) setUTCHours(hoursValue, minutesValue, secondsValue, msValue)
Parameters
-
hoursValue
- An integer between0
and23
representing the hours. -
minutesValue
Optional - An integer between0
and59
representing the minutes. -
secondsValue
Optional - An integer between0
and59
representing the seconds. If you specifysecondsValue
, you must also specifyminutesValue
. -
msValue
Optional - An integer between0
and999
representing the milliseconds. If you specifymsValue
, you must also specifyminutesValue
andsecondsValue
.
Return value
Changes the Date
object in place, and returns its new timestamp. If a parameter is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date
and NaN
is returned.
Description
If you do not specify the minutesValue
, secondsValue
, and msValue
parameters, the values returned from the getUTCMinutes()
, getUTCSeconds()
, and getUTCMilliseconds()
methods are used.
If a parameter you specify is outside of the expected range, setUTCHours()
attempts to update the date information in the Date
object accordingly. For example, if you use 100
for secondsValue
, the minutes will be incremented by 1
(minutesValue + 1
), and 40
will be used for seconds.
Examples:
const theBigDay = new Date(); theBigDay.setUTCHours(8);
“Date.prototype.setUTCHours() - JavaScript | MDN” (MDN Web Docs). Retrieved March 28, 2024.
Date.prototype.setUTCMilliseconds()
The setUTCMilliseconds()
method of Date
instances changes the milliseconds for this date according to universal time.
Syntax
setUTCMilliseconds(millisecondsValue)
Parameters
-
millisecondsValue
- An integer between0
and999
representing the milliseconds.
Return value
Changes the Date
object in place, and returns its new timestamp. If millisecondsValue
is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date
and NaN
is returned.
Description
If a parameter you specify is outside of the expected range, setUTCMilliseconds()
attempts to update the date information in the Date
object accordingly. For example, if you use 1100
for millisecondsValue
, the seconds stored in the Date
object will be incremented by 1
, and 100
will be used for milliseconds.
Examples:
const theBigDay = new Date(); theBigDay.setUTCMilliseconds(500);
“Date.prototype.setUTCMilliseconds() - JavaScript | MDN” (MDN Web Docs). Retrieved March 29, 2024.
Date.prototype.setUTCMinutes()
The setUTCMinutes()
method of Date
instances changes the minutes for this date according to universal time.
Syntax
setUTCMinutes(minutesValue) setUTCMinutes(minutesValue, secondsValue) setUTCMinutes(minutesValue, secondsValue, msValue)
Parameters
-
minutesValue
- An integer between0
and59
representing the minutes. -
secondsValue
Optional - An integer between0
and59
representing the seconds. If you specifysecondsValue
, you must also specifyminutesValue
. -
msValue
Optional - An integer between0
and999
representing the milliseconds. If you specifymsValue
, you must also specifyminutesValue
andsecondsValue
.
Return value
Changes the Date
object in place, and returns its new timestamp. If a parameter is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date
and NaN
is returned.
Description
If you do not specify the secondsValue
and msValue
parameters, the values returned from getUTCSeconds()
, and getUTCMilliseconds()
methods are used.
If a parameter you specify is outside of the expected range, setUTCMinutes()
attempts to update the date information in the Date
object accordingly. For example, if you use 100
for secondsValue
, the minutes will be incremented by 1
(minutesValue + 1
), and 40
will be used for seconds.
Examples:
const theBigDay = new Date(); theBigDay.setUTCMinutes(43);
“Date.prototype.setUTCMinutes() - JavaScript | MDN” (MDN Web Docs). Retrieved March 29, 2024.
Date.prototype.setUTCMonth()
The setUTCMonth()
method of Date
instances changes the month and/or day of the month for this date according to universal time.
Syntax
setUTCMonth(monthValue) setUTCMonth(monthValue, dateValue)
Parameters
-
monthValue
- An integer representing the month:0
for January,1
for February, and so on. -
dateValue
Optional - An integer from1
to31
representing the day of the month.
Return value
Changes the Date
object in place, and returns its new timestamp. If a parameter is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date
and NaN
is returned.
Description
If you do not specify the dateValue
parameter, the value returned from the getUTCDate()
method is used.
If a parameter you specify is outside of the expected range, setUTCMonth()
attempts to update the date information in the Date
object accordingly. For example, if you use 15
for monthValue
, the year will be incremented by 1
, and 3
will be used for month.
Examples:
const theBigDay = new Date(); theBigDay.setUTCMonth(11);
“Date.prototype.setUTCMonth() - JavaScript | MDN” (MDN Web Docs). Retrieved March 29, 2024.
Date.prototype.setUTCSeconds()
The setUTCSeconds()
method of Date
instances changes the seconds and/or milliseconds for this date according to universal time.
Syntax
setUTCSeconds(secondsValue) setUTCSeconds(secondsValue, msValue)
Parameters
-
secondsValue
- An integer between0
and59
representing the seconds. -
msValue
Optional - An integer between 0 and 999 representing the milliseconds.
Return value
Changes the Date object in place, and returns its new timestamp. If a parameter is NaN
(or other values that get coerced to NaN
, such as undefined
), the date is set to Invalid Date
and NaN
is returned.
Description
If you do not specify the msValue
parameter, the value returned from the getUTCMilliseconds()
method is used.
If a parameter you specify is outside of the expected range, setUTCSeconds()
attempts to update the date information in the Date object accordingly. For example, if you use 100
for secondsValue
, the minutes stored in the Date
object will be incremented by 1
, and 40
will be used for seconds.
Examples:
const theBigDay = new Date(); theBigDay.setUTCSeconds(20);
“Date.prototype.setUTCSeconds() - JavaScript | MDN” (MDN Web Docs). Retrieved April 2, 2024.
Date.prototype.toDateString()
The toDateString()
method of Date
instances returns a string representing the date portion of this date interpreted in the local timezone.
Syntax
toDateString()
Parameters
None.
Return value
A string representing the date portion of the given date. Returns "Invalid Date"
if the date is invalid.
Description
Date
instances refer to a specific point in time. toDateString()
interprets the date in the local timezone and formats the date part in English. It always uses the following format, separated by spaces:
- First three letters of the week day name
- First three letters of the month name
- Two-digit day of the month, padded on the left a zero if necessary
- Four-digit year (at least), padded on the left with zeros if necessary.
May have a negative sign
For example: "Thu Jan 01 1970"
.
- If you only want to get the time part, use
toTimeString()
. - If you want to get both the date and time, use
toString()
. - If you want to make the date interpreted as UTC instead of local timezone, use
toUTCString()
. - If you want to format the date in a more user-friendly format (e.g. localization), use
toLocaleDateString()
.
Examples:
const d = new Date(0); console.log(d.toString()); // "Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)" console.log(d.toDateString()); // "Thu Jan 01 1970"
“Date.prototype.toDateString() - JavaScript | MDN” (MDN Web Docs). Retrieved April 2, 2024.
Date.prototype.toISOString()
The toISOString()
method of Date
instances returns a string representing this date in the date time string format, a simplified format based on ISO 8601, which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ
or ±YYYYYY-MM-DDTHH:mm:ss.sssZ
, respectively). The timezone is always UTC, as denoted by the suffix Z
.
Syntax
toISOString()
Parameters
None.
Return value
A string representing the given date in the date time string format according to universal time. It’s the same format as the one required to be recognized by Date.parse()
.
Exceptions
-
RangeError
- Thrown if the date is invalid or if it corresponds to a year that cannot be represented in the date string format.
Examples
const d = new Date(0); console.log(d.toISOString()); // "1970-01-01T00:00:00.000Z"
“Date.prototype.toISOString() - JavaScript | MDN” (MDN Web Docs). Retrieved April 2, 2024.
Date.prototype.toJSON()
The toJSON()
method of Date
instances returns a string representing this date in the same ISO format as toISOString().
Syntax
toJSON()
Parameters
None.
Return value
A string representing the given date in the date time string format according to universal time, or null
when the date is invalid. For valid dates, the return value is the same as that of toISOString()
.
Examples:
const jsonDate = new Date(0).toJSON(); // '1970-01-01T00:00:00.000Z' const backToDate = new Date(jsonDate); console.log(jsonDate); // 1970-01-01T00:00:00.000Z
“Date.prototype.toJSON() - JavaScript | MDN” (MDN Web Docs). Retrieved April 2, 2024.
Date.prototype.toLocaleDateString()
The toLocaleDateString()
method of Date
instances returns a string with a language-sensitive representation of the date portion of this date in the local timezone. In implementations with Intl.DateTimeFormat
API support, this method simply calls Intl.DateTimeFormat
.
Syntax
toLocaleDateString() toLocaleDateString(locales) toLocaleDateString(locales, options)
Parameters
The locales and options parameters customize the behavior of the function and let applications specify the language whose formatting conventions should be used.
-
locales
Optional - A string with a BCP 47 language tag, or an array of such strings. Corresponds to the locales parameter of the Intl.DateTimeFormat() constructor. -
options
Optional - An object adjusting the output format. Corresponds to theoptions
parameter of theIntl.DateTimeFormat()
constructor. The timeStyle option must beundefined
, or aTypeError
would be thrown. Ifweekday
,year
,month
, andday
are allundefined
, thenyear
,month
, andday
will be set to"numeric"
.
See the Intl.DateTimeFormat()
constructor for details on these parameters and how to use them.
Return value
A string representing the date portion of the given date according to language-specific conventions.
Note: Most of the time, the formatting returned by toLocaleDateString()
is consistent. However, the output may vary with time, language, and implementation — output variations are by design and allowed by the specification. You should not compare the results of toLocaleDateString()
to static values.
Examples:
const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); // formats below assume the local time zone of the locale; // America/Los_Angeles for the US // US English uses month-day-year order console.log(date.toLocaleDateString("en-US")); // "12/20/2012" // British English uses day-month-year order console.log(date.toLocaleDateString("en-GB")); // "20/12/2012" // Korean uses year-month-day order console.log(date.toLocaleDateString("ko-KR")); // "2012. 12. 20." // Event for Persian, It's hard to manually convert date to Solar Hijri console.log(date.toLocaleDateString("fa-IR")); // "۱۳۹۱/۹/۳۰" // Arabic in most Arabic speaking countries uses real Arabic digits console.log(date.toLocaleDateString("ar-EG")); // "٢٠/١٢/٢٠١٢" // for Japanese, applications may want to use the Japanese calendar, // where 2012 was the year 24 of the Heisei era console.log(date.toLocaleDateString("ja-JP-u-ca-japanese")); // "24/12/20" // when requesting a language that may not be supported, such as // Balinese, include a fallback language, in this case Indonesian console.log(date.toLocaleDateString(["ban", "id"])); // "20/12/2012"
“Date.prototype.toLocaleDateString() - JavaScript | MDN” (MDN Web Docs). Retrieved April 3, 2024.j
Date.prototype.toLocaleString()
The toLocaleString()
method of Date
instances returns a string with a language-sensitive representation of this date in the local timezone.
Syntax
toLocaleString() toLocaleString(locales) toLocaleString(locales, options)
Parameters
The locales and options parameters customize the behavior of the function and let applications specify the language whose formatting conventions should be used.
-
locales
Optional - A string with a BCP 47 language tag, or an array of such strings. Corresponds to the locales parameter of the Intl.DateTimeFormat() constructor. -
options
Optional - An object adjusting the output format. Corresponds to the options parameter of theIntl.DateTimeFormat()
constructor. Ifweekday
,year
,month
,day
,dayPeriod
,hour
,minute
,second
, andfractionalSecondDigits
are all undefined, thenyear
,month
,day
,hour
,minute
,second
will be set to"numeric"
.
See the Intl.DateTimeFormat()
constructor for details on these parameters and how to use them.
Return value
A string representing the given date according to language-specific conventions.
In implementations with Intl.DateTimeFormat
, this is equivalent to new Intl.DateTimeFormat(locales, options).format(date)
.
Note: Most of the time, the formatting returned by toLocaleString() is consistent. However, the output may vary with time, language, and implementation — output variations are by design and allowed by the specification. You should not compare the results of toLocaleString()
to static values.
Examples:
const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); // Formats below assume the local time zone of the locale; // America/Los_Angeles for the US // US English uses month-day-year order and 12-hour time with AM/PM console.log(date.toLocaleString("en-US")); // "12/19/2012, 7:00:00 PM" // British English uses day-month-year order and 24-hour time without AM/PM console.log(date.toLocaleString("en-GB")); // "20/12/2012 03:00:00" // Korean uses year-month-day order and 12-hour time with AM/PM console.log(date.toLocaleString("ko-KR")); // "2012. 12. 20. 오후 12:00:00" // Arabic in most Arabic-speaking countries uses Eastern Arabic numerals console.log(date.toLocaleString("ar-EG")); // "٢٠/١٢/٢٠١٢ ٥:٠٠:٠٠ ص" // For Japanese, applications may want to use the Japanese calendar, // where 2012 was the year 24 of the Heisei era console.log(date.toLocaleString("ja-JP-u-ca-japanese")); // "24/12/20 12:00:00" // When requesting a language that may not be supported, such as // Balinese, include a fallback language (in this case, Indonesian) console.log(date.toLocaleString(["ban", "id"])); // "20/12/2012 11.00.00"
“Date.prototype.toLocaleString() - JavaScript | MDN” (MDN Web Docs). Retrieved April 3, 2024.
Date.prototype.toLocaleTimeString()
The toLocaleTimeString()
method of Date
instances returns a string with a language-sensitive representation of the time portion of this date in the local timezone. In implementations with Intl.DateTimeFormat
API support, this method simply calls Intl.DateTimeFormat
.
Syntax
toLocaleTimeString() toLocaleTimeString(locales) toLocaleTimeString(locales, options)
Parameters
The locales and options parameters customize the behavior of the function and let applications specify the language whose formatting conventions should be used.
-
locales
Optional - A string with a BCP 47 language tag, or an array of such strings. Corresponds to thelocales
parameter of theIntl.DateTimeFormat()
constructor. -
options
Optional - An object adjusting the output format. Corresponds to theoptions
parameter of theIntl.DateTimeFormat()
constructor. IfdayPeriod
,hour
,minute
,second
, andfractionalSecondDigits
are allundefined
, thenhour
,minute
,second
will be set to"numeric"
.
See the Intl.DateTimeFormat()
constructor for details on these parameters and how to use them.
Return value
A string representing the time portion of the given date according to language-specific conventions.
Note: Most of the time, the formatting returned by toLocaleTimeString()
is consistent. However, the output may vary with time, language, and implementation — output variations are by design and allowed by the specification. You should not compare the results of toLocaleTimeString()
to static values.
Examples:
const date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0)); // toLocaleTimeString() without arguments depends on the implementation, // the default locale, and the default time zone console.log(date.toLocaleTimeString()); // "7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles
“Date.prototype.toLocaleTimeString() - JavaScript | MDN” (MDN Web Docs). Retrieved April 4, 2024.
Date.prototype.toString()
Date.prototype.toString()
returns a string representation of the Date as interpreted in the local timezone, containing both the date and the time — it joins the string representation specified in toDateString()
and toTimeString()
together, adding a space in between. For example: "Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)"
.
Syntax
toString()
Parameters
None.
Return value
A string representing the given date. Returns "Invalid Date"
if the date is invalid.
Description
The Date
object overrides the toString() method of Object.
Examples:
const d = new Date(0); console.log(d.toString()); // "Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)"
“Date.prototype.toString()
- JavaScript | MDN” (MDN Web Docs). Retrieved April 4, 2024.
Date.prototype.toTimeString()
The toTimeString()
method of Date
instances returns a string representing the time portion of this date interpreted in the local timezone.
Date
instances refer to a specific point in time. toTimeString()
interprets the date in the local timezone and formats the time part in English. It always uses the format of hh:mm:ss GMT±xxxx (TZ)
, where:
-
hh
- Hour, as two digits with leading zero if required -
mm
- Minute, as two digits with leading zero if required -
ss
- Seconds, as two digits with leading zero if required -
±xxxx
- The local timezone’s offset — two digits for hours and two digits for minutes (e.g.-0500
,+0800
) -
TZ
- The timezone’s name (e.g.PDT
,PST
)
For example: "04:42:04 GMT+0000 (Coordinated Universal Time)".
Syntax
toTimeString()
Parameters
None.
Return value
A string representing the time portion of the given date (see description for the format). Returns "Invalid Date"
if the date is invalid.
Examples:
const d = new Date(0); console.log(d.toString()); // "Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)" console.log(d.toTimeString()); // "00:00:00 GMT+0000 (Coordinated Universal Time)"
“Date.prototype.toTimeString()
- JavaScript | MDN” (MDN Web Docs). Retrieved April 5, 2024.
Date.prototype.toUTCString()
The toUTCString()
method of Date
instances returns a string representing this date in the RFC 7231 format, with negative years allowed. The timezone is always UTC
. toGMTString()
is an alias of this method.
The value returned by toUTCString()
is a string in the form Www, dd Mmm yyyy hh:mm:ss GMT
, where:
-
Www
- Day of week, as three letters (e.g.Sun
,Mon
) -
dd
- Day of month, as two digits with leading zero if required -
Mmm
- Month, as three letters (e.g.Jan
,Feb
) -
yyyy
- Year, as four or more digits with leading zeroes if required -
hh
- Hour, as two digits with leading zero if required -
mm
- Minute, as two digits with leading zero if required -
ss
- Seconds, as two digits with leading zero if required
Syntax
toUTCString()
Parameters
None.
Return value
A string representing the given date using the UTC time zone (see description for the format). Returns "Invalid Date"
if the date is invalid.
Examples:
const d = new Date(0); console.log(d.toUTCString()); // 'Thu, 01 Jan 1970 00:00:00 GMT'
“Date.prototype.toUTCString()
- JavaScript | MDN” (MDN Web Docs). Retrieved April 5, 2024.
Date.UTC()
The Date.UTC()
static method accepts parameters representing the date and time components similar to the Date
constructor, but treats them as UTC
. It returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
If a parameter is outside of the expected range, the UTC()
method updates the other parameters to accommodate the value. For example, if 15
is used for monthIndex
, the year
will be incremented by 1
(year + 1
) and 3
will be used for the month.
The UTC()
method differs from the Date() constructor in three ways:
-
Date.UTC()
uses universal time instead of the local time. -
Date.UTC()
returns a time value as anumber
instead of creating aDate
object. - When passed a single number,
Date.UTC()
interprets it as a year instead of a timestamp.
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)
Parameters
-
year
- Integer value representing the year. Values from0
to99
map to the years1900
to1999
. All other values are the actual year. -
monthIndex
Optional - Integer value representing the month, beginning with0
for January to11
forDecember
. Defaults to0
. -
day
Optional - Integer value representing the day of the month. Defaults to1
. -
hours
Optional - Integer value between0
and23
representing the hour of the day. Defaults to0
. -
minutes
Optional - Integer value representing the minute segment of a time. Defaults to0
. -
seconds
Optional - Integer value representing the second segment of a time. Defaults to0
. -
milliseconds
Optional - Integer value representing the millisecond segment of a time. Defaults to0
.
Return value
A number representing the timestamp of the given date. Returns NaN
if the date is invalid.
Examples:
const utcDate = new Date(Date.UTC(2018, 11, 1, 0, 0, 0)); Date.UTC(2017); // 1483228800000
“Date.UTC()
- JavaScript | MDN” (MDN Web Docs). Retrieved April 5, 2024.
Date.prototype.valueOf()
The valueOf()
method of Date
instances returns the number of milliseconds for this date since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC.
Syntax
valueOf()
Parameters
None.
Return value
A number representing the timestamp, in milliseconds, of this date. Returns NaN
if the date is invalid.
Examples:
const d = new Date(0); // 1970-01-01T00:00:00.000Z console.log(d.valueOf()); // 0
“Date.prototype.valueOf()
- JavaScript | MDN” (MDN Web Docs). Retrieved April 5, 2024.