getTime vs valueOf on dates in JavaScript
Intermediate · 4 min read
Every date in JavaScript is a number underneath: its timestamp, the milliseconds since 1 January 1970 in UTC. There are four ways to get that number out, and they all give the same answer. Then there is a strange asymmetry: subtracting two dates gives a number, but adding two dates gives a long piece of text.
In this tutorial you will try each way of reading a date's number, see why - and + behave so differently with dates, and look at what happens to a date when it goes through JSON, which is how dates travel between a browser and a server. You need to know how to make a date and what typeof does.
Primitives and objects
A primitive is a simple value such as a number, a string or a boolean. A date is an object, not a primitive. When an operator needs a primitive, JavaScript asks the object to turn itself into one.
The object is told which kind the operator would like: a number, a string, or "no preference". Most objects give the same answer either way. Dates are special: with no preference, they give their string. That one rule explains everything in this exercise.
Which operators ask for what
-,*,/and<,>only work on numbers, so they ask for a number.+can add numbers or join strings, so it asks with no preference. If either side ends up a string,+joins.String(d)and template strings ask for a string.
Here is the exercise. If you want to try it first, go ahead. Otherwise read on and we will work through it together.
39 getTime, valueOf, and why adding two dates gives a string
fairShow the ways of getting a date's number out of it, and then the asymmetry: subtracting two dates gives a number, adding them gives text.
Stuck? Three hints, smallest first
- 1.
getTime(),valueOf(),Number(d)and+dall give the same number. - 2.
-converts both operands to numbers.+does not: it asks for a primitive with no preference. - 3. A date asked for a primitive with no preference returns its string, unlike almost every other object.
Step 1: four ways to get the number
Make two dates one day apart, at midnight UTC:
const start = new Date(Date.UTC(2026, 0, 15));
const end = new Date(Date.UTC(2026, 0, 16));
console.log(start.getTime()); // 1768435200000
console.log(start.valueOf()); // 1768435200000
console.log(Number(start)); // 1768435200000
console.log(+start); // 1768435200000
All four agree. valueOf() is the method JavaScript calls when it wants a number from an object, and for dates it returns the same as getTime(). Number(...) and the unary + both ask for a number. In your own code, getTime() is the clearest to read.
Step 2: subtraction gives a number
const difference = end - start; // number: - forces both sides to numbers
console.log(difference); // 86400000
console.log(typeof difference); // 'number'
- asks each date for a number, so this subtracts two timestamps. 86,400,000 milliseconds is one day. That is why date differences work so naturally.
Step 3: the wrong turn, adding two dates
Now swap the minus for a plus:
const joined = start + end; // string: + prefers the string form
console.log(typeof joined); // 'string'
console.log(joined === String(start) + String(end)); // true
+ asked with no preference, so each date gave its string, something like 'Thu Jan 15 2026 ...'. Two strings joined with + make one long string. The exact text depends on your time zone, but it is always a string.
You will rarely add two dates on purpose. The same thing happens, though, with date + 1 or date + offset: you get the date's text with a 1 stuck on the end, not a later date.
If you really want to add two timestamps, ask for the numbers first:
console.log(+start + +end); // 3536956800000
Step 4: dates in JSON
JSON is the text format most APIs use. JSON.stringify turns a date into its ISO text, in UTC:
console.log(JSON.stringify(start)); // '"2026-01-15T00:00:00.000Z"'
const back = JSON.parse(JSON.stringify(start));
console.log(typeof back); // 'string'
The extra double quotes show that it is a JSON string. And JSON.parse does not turn it back into a date. What comes back is just a string. If you send a date to a server and read it back, you have to turn it into a date again yourself.
Step 5: turning the text back into a date
Date.parse reads ISO text and returns the timestamp, so new Date(back) rebuilds the date:
console.log(Date.parse(back) === start.getTime()); // true
console.log(new Date(back).getTime() === start.getTime()); // true
The round trip loses nothing: ISO text keeps every millisecond.
The full solution
The panel prints the number from getTime, valueOf and Number, the difference of the two dates, the type of their sum and the JSON form. The checks confirm all of it, including that JSON gives back a string and that Date.parse undoes toISOString. Press Run to see the output.
const start = new Date(Date.UTC(2026, 0, 15));
const end = new Date(Date.UTC(2026, 0, 16));
const difference = end - start; // number: - forces both sides to numbers
const joined = start + end; // string: + prefers the string form
console.log('getTime :', start.getTime());
console.log('valueOf :', start.valueOf());
console.log('Number(d):', Number(start));
console.log('end - start:', difference, typeof difference);
console.log('start + end:', typeof joined, '<- two long date strings glued together');
console.log('JSON :', JSON.stringify(start));
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('valueOf is getTime', start.valueOf(), start.getTime());
test('and so is Number()', Number(start), start.getTime());
test('subtracting gives milliseconds, as a number', difference, 86400000);
test('typed as a number', typeof difference, 'number');
test('but adding gives a string, which is almost never what you want', typeof joined, 'string');
test('because a date prefers its string form', String(start), start.toString());
test('JSON.stringify uses toISOString instead, and quotes it', JSON.stringify(start), '"2026-01-15T00:00:00.000Z"');
test('so a date survives a JSON round trip as text, not as a Date', typeof JSON.parse(JSON.stringify(start)), 'string');
test('Date.parse undoes toISOString exactly', Date.parse(start.toISOString()), start.getTime());
Date is the one built-in whose Symbol.toPrimitive prefers a string when no preference is given. That is why date2 - date1 is a millisecond count and date1 + date2 is two long English date strings concatenated. If you want the sum of two timestamps, ask for the numbers: +date1 + +date2.
The JSON pair is worth remembering when data crosses a wire. JSON.stringify serialises a date through toISOString, so it goes out as UTC text — and JSON.parse does not turn it back into a Date. Whatever comes back is a string until you revive it yourself.
Common mistakes
- Adding to a date with
+.date + 1joins text. UsesetDate, or work withgetTime()numbers. - Expecting
JSON.parseto give back dates. It gives strings. Convert them withnew Date(text). - Mixing up
getTime()andgetDate().getTime()is the timestamp;getDate()is the day of the month.
Try it yourself
- Print
start + 1and+start + 1. Explain the difference. - Write
reviveDates(obj, keys)that turns the listed keys of a parsed JSON object back into dates. - Find the moment exactly halfway between
startandendusing their numbers.