Date.prototype.setTime()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
ThesetTime() method ofDate instances changes thetimestamp for this date, which is the number of milliseconds since theepoch, defined as the midnight at the beginning of January 1, 1970, UTC.
In this article
Try it
const launchDate = new Date("July 1, 1999, 12:00:00");const futureDate = new Date();futureDate.setTime(launchDate.getTime());console.log(futureDate);// Expected output: "Thu Jul 01 1999 12:00:00 GMT+0200 (CEST)"const fiveMinutesInMs = 5 * 60 * 1000;futureDate.setTime(futureDate.getTime() + fiveMinutesInMs);console.log(futureDate);// Expected output: "Thu Jul 01 1999 12:05:00 GMT+0200 (CEST)"// Note: your timezone may varySyntax
js
setTime(timeValue)Parameters
timeValueAn integer representing the new timestamp — the number of milliseconds since the midnight at the beginning of January 1, 1970, UTC.
Return value
Changes theDate object in place, and returns its newtimestamp. IftimeValue isNaN (or other values that getcoerced toNaN, such asundefined), the date is set toInvalid Date andNaN is returned.
Examples
>Using setTime()
js
const theBigDay = new Date("1999-07-01");const sameAsBigDay = new Date();sameAsBigDay.setTime(theBigDay.getTime());Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-date.prototype.settime> |