JavaScript Date valueOf() Method

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

What You’ll Learn

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

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.

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.

📝 Syntax

JavaScript
date.valueOf()

Parameters

  • None.

Return value

  • Epoch milliseconds for the date-time stored in the object.
  • NaN when the Date is invalid.

Related APIs

⚡ Quick Reference

GoalCode
Epoch ms from Datedate.valueOf()
Same via getTimedate.getTime()
Numeric coercionNumber(date) or +date
Compare instantsa.valueOf() < b.valueOf()
Elapsed msend.valueOf() - start.valueOf()
Invalid DateNaN

📋 valueOf() vs getTime() vs Coercion

Three ways to get the same millisecond value from a Date.

valueOf()
date.valueOf()

Primitive hook

getTime()
date.getTime()

Explicit API

Number()
Number(date)

Calls valueOf

Subtract
b - a

Auto numeric

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Verify timestamps with toISOString() when helpful.

📚 Getting Started

Read epoch milliseconds from a Date instance.

Example 1 — Get the Numeric Timestamp

Call valueOf() on a fixed UTC instant.

JavaScript
const date = new Date("2026-03-15T12:00:00.000Z");

const timestamp = date.valueOf();

console.log(timestamp);           // 1773576000000 (example engine value)
console.log(date.toISOString());  // "2026-03-15T12:00:00.000Z"
Try It Yourself

How It Works

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
Try It Yourself

How It Works

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");
}
Try It Yourself

How It Works

Smaller epoch numbers represent earlier moments. You can also write date1 < date2—JavaScript coerces both sides to numbers via valueOf().

Example 4 — Implicit Coercion with Number()

Numeric conversion calls valueOf() under the hood.

JavaScript
const date = new Date("2026-01-01T00:00:00.000Z");

console.log(Number(date));
console.log(+date);
console.log(Number(date) === date.valueOf());  // true
Try It Yourself

How It Works

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.

JavaScript
const start = new Date("2026-01-01T00:00:00.000Z");
const end = new Date("2026-07-01T00:00:00.000Z");

const diffMs = end.valueOf() - start.valueOf();
const diffDays = diffMs / (1000 * 60 * 60 * 24);

console.log(diffMs);
console.log(diffDays);  // 181
Try It Yourself

How It Works

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).

🚀 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 detectionNumber.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.

📝 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.

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 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 valueOf() Excellent

Bottom line: Safe everywhere. Remember NaN for invalid dates and prefer getTime() for explicit reads.

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.

You have reached the end of the Date methods series in this track. Continue to JavaScript String methods, or review getTime() and Date.now().

💡 Best Practices

✅ Do

  • Use getTime() for explicit timestamp reads
  • Validate with Number.isNaN(date.getTime())
  • Subtract dates for elapsed millisecond diffs
  • Store epoch ms in APIs and databases when needed
  • 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)

Key Takeaways

Knowledge Unlocked

Five things to remember about valueOf()

Primitive timestamps and automatic numeric conversion.

5
Core concepts
🔢 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.

Continue to String methods

Move on from Date objects to JavaScript string manipulation.

String methods →

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