JavaScript Date setTime() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Epoch milliseconds

What You’ll Learn

setTime() writes the full instant on a Date using epoch milliseconds—the same number getTime() returns. This guide covers syntax, shifting by duration, copying timestamps, and comparisons with field setters.

01

Syntax

setTime(ms)

02

Epoch

Unix ms

03

Full instant

Not fields

04

+ duration

getTime + n

05

Mutates

In place

06

Returns

Same ms

Introduction

APIs, databases, and Date.now() give you millisecond timestamps. When you already have that number, field setters like setHours() are awkward—setTime(ms) sets the entire instant in one step.

The value is milliseconds since 1 January 1970 00:00:00 UTC (Unix epoch). Like other setters, it mutates the Date and returns the same numeric value as getTime() afterward.

Understanding the setTime() Method

Date.prototype.setTime(milliseconds) replaces every calendar and clock field at once. Local getters (getHours, getDate, etc.) reflect the new instant in your timezone; toISOString() shows the UTC representation.

To shift by a duration, add milliseconds first: date.setTime(date.getTime() + offsetMs). To copy one Date to another: target.setTime(source.getTime()).

💡
Beginner Tip

One day = 86_400_000 ms. Seven days later: date.setTime(date.getTime() + 7 * 86_400_000).

📝 Syntax

JavaScript
dateObj.setTime(milliseconds)

Parameters

  • milliseconds — Epoch milliseconds since 1970-01-01 UTC (same unit as getTime() and Date.now()).

Return value

  • The updated epoch milliseconds (identical to date.getTime() after the call).
  • NaN if the argument is NaN (Date becomes invalid).

⚡ Quick Reference

GoalCode
Set from epoch msdate.setTime(1_700_000_000_000)
Unix epoch startdate.setTime(0)
Add 24 hoursdate.setTime(date.getTime() + 86_400_000)
Copy another Datecopy.setTime(original.getTime())
From ISO via parsedate.setTime(Date.parse(iso))
Check invalidNumber.isNaN(date.getTime())

📋 setTime() vs Similar Methods

Use setTime() when you have a full timestamp; use field setters for wall-clock tweaks.

setTime()
full ms

Whole instant

getTime()
read

Getter pair

Date.now()
now ms

Current timestamp

setHours()
one field

Field setter

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Epoch 0 is always 1970-01-01T00:00:00.000Z in UTC.

📚 Getting Started

Set a Date from a known millisecond timestamp.

Example 1 — Set the Unix Epoch (setTime(0))

Jump to the origin instant: 1 January 1970 UTC.

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

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

How It Works

setTime(0) sets the full instant at once. Local getters show the equivalent wall time in your timezone.

📈 Practical Patterns

Duration shifts, copying instants, return values, and parsed timestamps.

Example 2 — Add Twenty-Four Hours

Shift forward one day by adding milliseconds to getTime().

JavaScript
const date = new Date(2026, 2, 1, 12, 0, 0);
const oneDayMs = 24 * 60 * 60 * 1000;
date.setTime(date.getTime() + oneDayMs);

console.log(date.getDate());  // 2
console.log(date.getHours()); // 12
Try It Yourself

How It Works

This is the correct pattern for “N days later”—add ms to getTime(), then pass the sum to setTime().

Example 3 — Copy an Instant to Another Date

Duplicate a timestamp without sharing the same object reference.

JavaScript
const source = new Date(2026, 5, 15, 10, 30, 0);
const copy = new Date(0);
copy.setTime(source.getTime());

console.log(copy.getTime() === source.getTime());  // true
console.log(copy === source);                      // false
Try It Yourself

How It Works

new Date(source.getTime()) also works; setTime is useful when reusing an existing Date variable.

Example 4 — Return Value Matches getTime()

setTime returns epoch ms—the same number the Date now holds.

JavaScript
const date = new Date(2026, 0, 1);
const returned = date.setTime(1_735_689_600_000);

console.log("Type:", typeof returned);                 // "number"
console.log("Matches getTime():", returned === date.getTime()); // true
Try It Yourself

How It Works

Do not chain field setters on the return value—mutate the same date variable instead.

Example 5 — Apply a Parsed ISO Timestamp

Combine Date.parse() with setTime() for API strings.

JavaScript
const iso = "2026-06-15T09:30:00.000Z";
const ms = Date.parse(iso);
const date = new Date();
date.setTime(ms);

console.log(date.toISOString());  // "2026-06-15T09:30:00.000Z"

const bad = Date.parse("not-a-date");
console.log(Number.isNaN(bad));   // true
Try It Yourself

How It Works

Validate with Number.isNaN(ms) before calling setTime—passing NaN creates an invalid Date.

🚀 Common Use Cases

  • API timestamps — apply server epoch ms to a Date object.
  • Duration shifts — add days/hours via getTime() + offset.
  • Clone instantscopy.setTime(original.getTime()).
  • Reset to known instant — snap back to a saved timestamp.
  • Benchmarks — store start/end ms, then setTime for replay.
  • Invalid guard — skip setTime when parse returns NaN.

🧠 How setTime() Updates the Instant

1

Receive epoch ms

Argument is milliseconds since 1970-01-01 UTC.

Input
2

Replace instant

All calendar and clock fields derive from the new ms value.

setTime
3

Handle NaN

setTime(NaN) marks the Date invalid.

Invalid
4

Mutate & return ms

Return value equals getTime() on success.

📝 Notes

  • setTime() sets the full instant—not just the time-of-day.
  • Same unit as getTime(), Date.now(), and Date.parse() results.
  • For midnight local snap, use setHours(0, 0, 0, 0)—not setTime unless you compute the ms.
  • Mutates the original Date—clone when preserving the old instant.
  • Returns epoch ms, not the Date—no fluent chaining on the return value.
  • Validate parsed values with Number.isNaN() before calling setTime.

Browser & Runtime Support

Date.prototype.setTime() has been available since ES1. It works in every browser and Node.js.

Baseline · ES1

Date.prototype.setTime()

Supported in Chrome, Firefox, Safari, Edge, Internet Explorer, and all Node.js versions. No polyfill required.

99% Universal API
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. Pair with getTime() for read/write and duration math.

Conclusion

setTime() writes the full epoch-millisecond instant on a JavaScript Date. Use it when you have a Unix timestamp; use field setters when tweaking individual calendar or clock parts.

Next, learn setUTCDate() for UTC day writes, or review getTime() for reading timestamps.

💡 Best Practices

✅ Do

  • Use setTime(getTime() + ms) for duration shifts
  • Pair with getTime() for read/write symmetry
  • Validate API ms with Number.isFinite(ms)
  • Clone before mutating shared Date instances
  • Prefer new Date(ms) when creating fresh objects

❌ Don’t

  • Confuse with setSeconds() (clock second, not epoch)
  • Use loose isNaN on strings—use Number.isNaN
  • Chain field setters on the numeric return value
  • Assume setTime resets to local midnight
  • Skip validation on parsed or user-supplied timestamps

Key Takeaways

Knowledge Unlocked

Five things to remember about Date.setTime()

Your foundation for epoch millisecond writes in JavaScript.

5
Core concepts
🔢 02

Epoch ms

Full instant.

Unit
🔄 03

getTime pair

Read/write.

Pattern
04

+ duration

getTime + n.

Shift
05

NaN

Invalid Date.

Guard

❓ Frequently Asked Questions

It sets a Date object to the instant represented by epoch milliseconds—the same numeric value getTime() returns. The Date mutates in place and returns that millisecond value.
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 calendar or clock component (with overflow rules). setTime() replaces the entire instant in one call—ideal when you already have a Unix timestamp from an API.
Yes. date.setTime(date.getTime() + 7 * 24 * 60 * 60 * 1000) shifts the instant forward by seven days in milliseconds.
No. It returns a number (the epoch milliseconds you set). The Date object itself is updated—do not chain field setters on the return value.
The Date becomes invalid. getTime() and setTime() both return NaN, and Number.isNaN(date.getTime()) is true.
Did you know?

setTime() and getTime() use the same epoch milliseconds—after date.setTime(ms), the return value always equals date.getTime(). That is different from setSeconds(), which only changes the clock’s second hand.

Continue to setUTCDate()

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

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.

6 people found this page helpful