73

MDN says that valueOf and getTime are functionally equivalent. Why have two functions that do the very same thing?

3 Answers 3

84

The Date.prototype.getTime method returns the number of milliseconds since the epoch (1970-01-01T00:00:00Z); it is unique to the Date type and an important method.

The Object.prototype.valueOf method is used to get the "primitive value" of any object and is used by the language internally when it needs to convert an object to a primitive. For the Date class, it is convenient to use the "time" attribute (the value returned by getTime()) as its primitive form since it is a common representation for dates. Moreover, it lets you use arithmetic operators on date objects so you can compare them simply by using comparison operators (<, <=, >, etc).

var d = new Date();
d.getTime(); // => 1331759119227
d.valueOf(); // => 1331759119227
+d; // => 1331759119227 (implicitly calls "valueOf")
var d2 = new Date();
(d < d2); // => true (d came before d2)

Note that you could implement the "valueOf" method for your own types to do interesting things:

function Person(name, age) {this.name=name; this.age=age;}
Person.prototype.valueOf = function() {return this.age; }

var youngster = new Person('Jimmy', 12);
var oldtimer = new Person('Hank', 73);
(youngster < oldtimer); // => true
youngster + oldtimer; // => 85
5
  • 4
    Be aware that +d is not the same as 0+d. The latter converts Date to String.
    – fdermishin
    Sep 1, 2012 at 18:02
  • 1
    I'm sure this is a prefectly good answer, but it's still a little fuzzy for me. Would you be able to elaborate a little? I'm confused by "get the 'primitive type'" and "time...as it's 'primitive form'". How is this a distinction between getTime() and valueOf()? ALso, what is meant by "scaled" representation? Thank you.
    – Karl
    Nov 18, 2012 at 19:16
  • 3
    @Karl, the sole purpose of getTime() is to return the number of milliseconds since midnight 01 January, 1970 UTC. Whereas valueOf() returns a single value - it might be a string, a number but not an object class - hence primitive type. Something that might be used for comparisons, a numeric representation. I suppose the language designers thought you can already use getTime() for that purpose and just used it. It might have been implemented differently but for convenience they used getTime(). Jan 20, 2014 at 9:28
  • 4
    Note that while d < d2 will work, d == d2 returns false for any d, d2. Therefore d <= d2 and d >= d2 will not treat equal values correctly. To properly compare equal values, you need to use either d.valueOf() <= d2.valueOf() or its getTime equivalent.
    – jxg
    Jul 31, 2014 at 18:47
  • @jxg You are wrong in parts. All relational operators (<, <=, >, >=) turn their operands to primitives and work fine with Date objects. The equality operators (===, !===, ==, !=) do not coerce, if both operands are objects, and thus fail to do what is intended. So it would indeed be possible to use d <= d2 && d2 <= d to compare for date equality. (Btw, to avoid bugs, never use == and != in Javascript.) Mar 7, 2020 at 18:43
26

There are no difference in behaviour between those two functions:

v8 Source Code on tag 4.8.47 in /src/date.js:

// ECMA 262 - 15.9.5.8
function DateValueOf() {
  CHECK_DATE(this);
  return UTC_DATE_VALUE(this);
}
// ECMA 262 - 15.9.5.9
function DateGetTime() {
  CHECK_DATE(this);
  return UTC_DATE_VALUE(this);
}

But there are historical differences.

0
8

valueOf is a method of all objects. Objects are free to override this to be what they want.

1
  • 10
    This makes sense if we consider that getTime got introduced in JavaScript 1.0 and valueOf was introduced in JavaScript 1.1. My guess is that getTime was kept for backwards compability.
    – Attila Kun
    Mar 14, 2012 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.