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
Fundamentals
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.
Concept
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).
Foundation
📝 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).
Cheat Sheet
⚡ Quick Reference
Goal
Code
Set from epoch ms
date.setTime(1_700_000_000_000)
Unix epoch start
date.setTime(0)
Add 24 hours
date.setTime(date.getTime() + 86_400_000)
Copy another Date
copy.setTime(original.getTime())
From ISO via parse
date.setTime(Date.parse(iso))
Check invalid
Number.isNaN(date.getTime())
Compare
📋 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
Hands-On
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"
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
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.
Important
📝 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.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Date.setTime()Excellent
Bottom line: Safe everywhere. Pair with getTime() for read/write and duration math.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Date.setTime()
Your foundation for epoch millisecond writes in JavaScript.
5
Core concepts
📝01
Syntax
setTime(ms)
API
🔢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.