Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Date.prototype.setTime: replace the time value

setTime replaces a Date object’s time value with epoch milliseconds, applies TimeClip, and returns the new value.

Version: ECMAScript 1 onward; example target: Node.js 24.22.2

Syntax

date.setTime(timeValue)

date.setTime(timeValue) converts its argument with ToNumber, truncates a fractional result toward zero, and stores it as milliseconds since 1970-01-01T00:00:00Z. The operation does not depend on local time zone or previous calendar fields. It returns the stored millisecond value, which then equals getTime().

A non-finite value, NaN, a missing argument, or an absolute value above 8.64e15 invalidates the date and returns NaN. A later call with a valid value can restore an invalid Date. BigInt and Symbol throw TypeError during ToNumber; object coercion can also throw.

A receiver without Date internal state throws TypeError, and extra arguments are ignored. Use setTime to restore a timestamp or shift an instant by a millisecond delta. The method has existed since ECMAScript 1.

Examples

const date = new Date(0);
console.log(date.setTime(1704067200123.9));
console.log(date.toISOString());

console.log(date.setTime(8.64e15 + 1), Number.isNaN(date.getTime()));
console.log(date.setTime(0), date.toISOString());

Result

1704067200123
2024-01-01T00:00:00.123Z
NaN true
0 1970-01-01T00:00:00.000Z

Sources