valueOf() returns a Date’s instant as epoch milliseconds—the same number as getTime(). It also powers automatic numeric conversion when you subtract dates or use Number(date).
01
Syntax
date.valueOf()
02
Returns
Epoch ms
03
= getTime
Same value
04
Coercion
Number(d)
05
Compare
< and -
06
Invalid
NaN
Fundamentals
Introduction
A Date object represents one moment in time internally as milliseconds since the Unix epoch. Methods like getFullYear() expose calendar fields, but sometimes you need the raw number instead.
valueOf() is the generic “give me the primitive value” hook inherited from Object.prototype. On dates, that primitive is always the epoch millisecond timestamp. Understanding it explains why date1 - date2 just works.
Concept
Understanding the valueOf() Method
Date.prototype.valueOf() converts the instance to a number:
Return type — primitive number (not a Date, not a string).
Unit — milliseconds since 1970-01-01 00:00:00 UTC.
Equivalence — identical to getTime() for valid Date objects.
Coercion — called when JavaScript needs a numeric Date (subtraction, Number(), unary +).
Invalid dates — returns NaN.
💡
Beginner Tip
In everyday code, date.getTime() reads clearer when you want a timestamp. Reach for valueOf() when learning coercion or reading code that compares dates numerically.
Foundation
📝 Syntax
JavaScript
date.valueOf()
Parameters
None.
Return value
Epoch milliseconds for the date-time stored in the object.
The large integer is not a clock component—it is the full instant encoded as milliseconds since the epoch. Pair with ISO output to connect the number back to a readable date.
📈 Practical Patterns
Equivalence, comparison, coercion, and diffs.
Example 2 — valueOf() Equals getTime()
Both methods read the same internal millisecond value.
JavaScript
const date = new Date("2026-07-17T08:00:00.000Z");
console.log(date.valueOf());
console.log(date.getTime());
console.log(date.valueOf() === date.getTime()); // true
getTime() is the Date-specific name added for clarity. valueOf() exists because every object can expose a primitive conversion hook.
Example 3 — Compare Two Dates Numerically
Order instants by comparing their valueOf() results.
JavaScript
const date1 = new Date("2022-01-01T00:00:00.000Z");
const date2 = new Date("2023-01-01T00:00:00.000Z");
if (date1.valueOf() < date2.valueOf()) {
console.log("date1 is earlier than date2");
} else if (date1.valueOf() > date2.valueOf()) {
console.log("date1 is later than date2");
} else {
console.log("same instant");
}
When an object is converted to a number, the engine calls valueOf() first. That is why +date gives you the same timestamp without an explicit method call.
Example 5 — Calculate Elapsed Milliseconds
Subtract two dates to measure the span between instants.
Subtraction coerces both Date objects to numbers. The difference is elapsed milliseconds—divide by 86_400_000 for whole days (watch leap seconds and DST only affect calendar getters, not epoch math).
Applications
🚀 Common Use Cases
Sort events — order logs or records by valueOf().
Deadline checks — compare Date.now() with a stored Date.
Elapsed timing — subtract start/end timestamps after an operation.
Serialization prep — store numeric instants in databases or JSON numbers.
Understanding coercion — debug why date + 1 behaves differently from date - 1.
Invalid detection — Number.isNaN(date.valueOf()) flags bad parses.
🧠 How Numeric Coercion Uses valueOf()
1
Need a number
Subtraction, Number(), or > triggers ToNumber on the Date.
Coercion
2
Call valueOf()
The engine invokes date.valueOf() and receives epoch ms.
Primitive
3
Use the number
Compare, subtract, or store the millisecond value.
Arithmetic
4
🔢
Same as getTime
Explicit getTime() skips coercion ceremony and returns the same value directly.
Important
📝 Notes
valueOf() returns milliseconds, not seconds.
For valid dates, valueOf() === getTime() is always true.
Invalid dates yield NaN—check before comparing or sorting.
date + 1 converts to string first (uses toString()), not valueOf().
date - 1 uses numeric coercion via valueOf().
Prefer getTime() in new code when readability matters.
Compatibility
Browser & Runtime Support
Date.prototype.valueOf() has been part of JavaScript since ES1 and behaves consistently everywhere.
✓ Baseline · ES1+
Date valueOf()
Supported in all browsers and Node.js versions. Core Date API alongside getTime().
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 valueOf()Excellent
Bottom line: Safe everywhere. Remember NaN for invalid dates and prefer getTime() for explicit reads.
Wrap Up
Conclusion
valueOf() exposes a Date’s epoch milliseconds and drives numeric coercion when you compare or subtract dates. It matches getTime() exactly—choose the name that best fits your reader’s intent.
Compare instants with < after confirming valid dates
❌ Don’t
Assume date + 1 adds one millisecond
Compare invalid dates without an NaN check
Divide timestamps by 1000 without Math.floor when you need integers
Feature-detect valueOf—it always exists on Date
Confuse epoch ms with getSeconds() (0–59)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about valueOf()
Primitive timestamps and automatic numeric conversion.
5
Core concepts
📝01
Syntax
date.valueOf()
API
🔢02
Epoch ms
Since 1970.
Unit
🔄03
= getTime
Same value.
Equiv
➕04
Coercion
Number(d).
Auto
❌05
Invalid
NaN.
Guard
❓ Frequently Asked Questions
The number of milliseconds since January 1, 1970 00:00:00 UTC for that Date instance—the same epoch timestamp as getTime(). It returns a primitive number, not a Date object.
Yes. For standard Date objects they return the same millisecond value. getTime() is the Date-specific name; valueOf() is the generic Object.prototype hook used when JavaScript coerces a Date to a number.
When a Date is converted to a number—Number(date), +date, date - otherDate, or comparison with > and <. The engine invokes valueOf() (or falls back to toString()) during that coercion.
Yes. dateA.valueOf() < dateB.valueOf() compares instants numerically. Subtracting two Dates (dateB - dateA) also works because both sides coerce to numbers via valueOf().
NaN—the same as getTime() on an invalid Date. Use Number.isNaN(date.valueOf()) or Number.isNaN(date.getTime()) to detect invalid dates.
Prefer getTime() when you explicitly want a timestamp from a Date—it reads clearly. valueOf() matters most when you rely on numeric coercion or study how Date objects convert to primitives.
Did you know?
date1 - date2 works because both operands convert to numbers through valueOf(), but date1 + date2 concatenates strings because + prefers string conversion when either side is a string.