JavaScript Date setTime() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Date

Definition

Date.prototype.setTime() changes the timestamp for this date — the number of milliseconds since the Unix epoch (midnight, January 1, 1970, UTC) (MDN). It mutates the Date in place and returns the new timestamp.

Remember
date.setTime(timeValue)

timeValue → milliseconds since 1970-01-01T00:00:00.000Z
→ returns new timestamp (ms)
→ mutates date

setTime(0)                         // Unix epoch
setTime(other.getTime())           // copy instant
setTime(date.getTime() + 5*60*1000) // +5 minutes

Syntax

Call date.setTime. Start with this line.

JavaScript
date.setTime(timeValue)
CallWhat it does
date.setTime(timeValue)Primary signature.
date.setTime(...)Call with the arguments your case needs.

Built into JavaScript. Call setTime(...) on a Date instance — no import required.

Library declarations
JavaScript
date.setTime(timeValue)

Parameters

ParameterDescription
timeValueRequired. Epoch milliseconds (UTC-based instant)

Return Value

Return ValueDescription
*New epoch ms (number), not the Date

JavaScript Examples

Example 1 — Copy an Instant

MDN-style: assign one Date’s timestamp onto another with setTime().

JavaScript
const launchDate = new Date("July 1, 1999, 12:00:00");
const futureDate = new Date();
futureDate.setTime(launchDate.getTime());

console.log(futureDate.getTime() === launchDate.getTime()); // true
console.log(futureDate.getFullYear()); // 1999
Try It Yourself

How It Works

  1. Source. launchDate.getTime() reads the epoch ms.
  2. Write. futureDate.setTime(...) sets that same instant.
  3. Equal. Both Dates now share the same getTime() value.
  4. Alt. new Date(launchDate.getTime()) also builds a copy in one step.

Example 2 — Add Five Minutes

Shift by a fixed millisecond delta (MDN timing pattern).

JavaScript
const d = new Date("July 1, 1999, 12:00:00");
const before = d.getTime();
const fiveMinutesInMs = 5 * 60 * 1000;

d.setTime(before + fiveMinutesInMs);

console.log(d.getTime() - before); // 300000
console.log(fiveMinutesInMs);      // 300000
Try It Yourself

How It Works

  1. Delta. Five minutes = 5 * 60 * 1000 → 300000 ms.
  2. Read + add. Take before, add the delta.
  3. Write. setTime(...) stores the new instant.
  4. Why it helps. Fixed ms math avoids DST surprises from local field setters.

Example 3 — Set the Unix Epoch

Pass 0 to jump to midnight, January 1, 1970 UTC.

JavaScript
const d = new Date();
d.setTime(0);

console.log(d.getTime());      // 0
console.log(d.toISOString());  // "1970-01-01T00:00:00.000Z"
Try It Yourself

How It Works

  1. Start from now. Any Date can be overwritten.
  2. Write zero. setTime(0) is exactly the Unix epoch.
  3. Read back. getTime() returns 0.
  4. ISO. toISOString() shows the UTC string for that instant.

Browser & Runtime Support

Date.prototype.setTime() is part of the language from early ECMAScript and is Baseline Widely available (MDN).

✓ Baseline · Widely available

Date.prototype.setTime()

Supported in every modern browser and Node.js. No polyfill needed for ordinary web apps.

99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Date.setTime() Excellent

Bottom line: Safe everywhere. Focus on mutation, Invalid Date from NaN, and choosing setTime vs field setters.

Frequently Asked Questions

It sets a Date to the instant represented by epoch milliseconds — milliseconds since January 1, 1970 00:00:00 UTC. The Date mutates in place and the method returns that millisecond number.
getTime() reads the current epoch milliseconds. setTime(ms) writes a new instant from epoch milliseconds. They are the read/write pair for the full timestamp.
Field setters change one local/UTC clock component (with overflow). setTime() replaces the entire instant in one call — ideal when you already have a Unix timestamp.
Yes. date.setTime(date.getTime() + 5 * 60 * 1000) shifts the instant forward by five minutes in milliseconds (MDN).
No. It returns a number (epoch milliseconds). Call setters on the Date variable — do not chain on the return value.
The Date becomes invalid. setTime() returns NaN, and Number.isNaN(date.getTime()) is true.

Next: setUTCDate()

Learn how to set the day of the month in UTC on a Date.

setUTCDate() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful