getTime() returns the number of milliseconds since the Unix epoch (Jan 1, 1970 UTC) for a Date instant. This guide covers syntax, duration math, timestamp round-trips, comparisons, and how it differs from getMilliseconds().
01
Syntax
date.getTime()
02
Epoch ms
Large integer
03
Duration
Subtract timestamps
04
Round-trip
new Date(ms)
05
vs now
Date.now()
06
Invalid
Returns NaN
Fundamentals
Introduction
APIs, databases, and sort functions often store time as a single number. The getTime() method exposes that number from any Date object—milliseconds elapsed since January 1, 1970 00:00:00 UTC.
Use it for elapsed duration, ordering events, and serializing instants. For the current timestamp without building a Date, see Date.now().
Concept
Understanding the getTime() Method
Date.prototype.getTime() is a zero-argument instance method. It never mutates the original Date—it returns the stored instant as a numeric timestamp.
The same instant always yields the same getTime() value regardless of local timezone. That makes it ideal for comparison and arithmetic; local getters like getHours() are for display, not for measuring duration.
💡
Beginner Tip
Subtracting two Date objects (end - start) works because JavaScript coerces them to numbers via getTime().
Foundation
📝 Syntax
JavaScript
dateObj.getTime()
Parameters
None.
Return value
Number of milliseconds since the Unix epoch for a valid Date.
NaN if dateObj is an invalid Date.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Current timestamp
new Date().getTime() or Date.now()
Elapsed milliseconds
end.getTime() - start.getTime()
Date from timestamp
new Date(1721203200000)
Which is earlier?
a.getTime() < b.getTime()
Invalid date check
Number.isNaN(date.getTime())
Same instant?
a.getTime() === b.getTime()
Compare
📋 getTime() vs Similar APIs
These APIs all involve milliseconds but serve different jobs. Pick the one that matches your task.
getTime()
epoch ms
Instance method
Date.now()
now ms
Static shortcut
valueOf()
same ms
Coercion hook
getMilliseconds()
0–999
Sub-second only
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Sample timestamps will differ on your machine when reading “now.”
📚 Getting Started
Read the epoch milliseconds from the current time.
Example 1 — Get the Current Timestamp
Call getTime() on a new Date() to read milliseconds since the epoch.
JavaScript
const now = new Date();
const timestamp = now.getTime();
console.log("Timestamp (ms):", timestamp);
getTime() and new Date(ms) are inverse operations for the same instant. JSON APIs often store timestamps this way.
Example 4 — Compare Which Date Is Earlier
Sort or compare events using numeric timestamps.
JavaScript
const meetingA = new Date(2026, 6, 4, 10, 0);
const meetingB = new Date(2026, 6, 4, 14, 30);
if (meetingA.getTime() < meetingB.getTime()) {
console.log("Meeting A is earlier.");
} else {
console.log("Meeting B is earlier or same.");
}
Date.now() is equivalent to new Date().getTime() but skips allocating a Date object. Both return epoch milliseconds suitable for duration math.
Applications
🚀 Common Use Cases
API payloads — store createdAt as epoch ms.
Sorting events — order by getTime().
Cooldown timers — compare now vs last action timestamp.
Session expiry — Date.now() > expiresAt.
Benchmarks — measure code block duration.
Cache keys — bust caches with timestamp suffixes.
🧠 How getTime() Represents an Instant
1
Date object
Internally holds one instant as UTC milliseconds.
Storage
2
getTime()
Returns that number directly—no timezone conversion.
Read
3
Math
Subtract, compare, or serialize the timestamp.
Use
4
⏱
Rebuild
new Date(ms) recreates the same instant.
Important
📝 Notes
getTime() equals valueOf() on valid dates.
Invalid dates return NaN—use that for validation.
Not the same as getMilliseconds() (0–999 sub-second).
Prefer Date.now() when you only need the current ms once.
Divide carefully when converting ms to days (watch DST if using local midnight tricks).
For high-precision benchmarks in browsers, consider performance.now() (relative, not epoch).
Compatibility
Browser & Runtime Support
Date.prototype.getTime() has been available since the first JavaScript Date implementation (ES1). It works in every browser and Node.js.
✓ Baseline · ES1
Date.prototype.getTime()
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.getTime()Excellent
Bottom line: Safe everywhere. Store epoch ms for APIs; format to local strings only at the UI boundary.
Wrap Up
Conclusion
getTime() is the standard way to read a Date as epoch milliseconds. Use it for duration, comparison, and serialization; use Date.now() for a quick “now” snapshot.
Rebuild with new Date(ms) when parsing stored values
❌ Don’t
Confuse getTime() with getMilliseconds()
Compare local date parts when getTime() suffices
Assume ISO strings parse the same in every timezone
Feature-detect getTime() in modern apps
Display raw epoch ms to end users without formatting
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Date.getTime()
Your foundation for timestamps in JavaScript.
5
Core concepts
📝01
Syntax
date.getTime()
API
🔢02
Epoch ms
Since 1970.
Format
🔄03
Subtract
Duration.
Math
🛠04
Rebuild
new Date(ms)
Round-trip
⚠05
Not getMs
0–999 vs epoch.
Pitfall
❓ Frequently Asked Questions
The number of milliseconds since January 1, 1970 00:00:00 UTC (the Unix epoch) for that Date instant. It is a large integer, not a 0–59 clock component.
getTime() is called on a Date instance. Date.now() is a static method that returns the current epoch milliseconds without creating a Date object. For 'now', they match: new Date().getTime() === Date.now().
getTime() returns full epoch milliseconds (e.g. 1721203200000). getMilliseconds() returns only 0–999 for the sub-second fraction within the current second.
NaN. That is the standard way to detect invalid dates: Number.isNaN(date.getTime()).
No. Two Date objects representing the same instant return the same getTime() value everywhere. Local getters like getHours() can differ; getTime() identifies the instant.
Subtract their timestamps: later.getTime() - earlier.getTime(). The result is elapsed milliseconds. Divide by 1000 for seconds, 60000 for minutes, etc.
Did you know?
When you write end - start with two Date objects, JavaScript calls valueOf(), which returns the same number as getTime(). That is why date subtraction gives elapsed milliseconds.