Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Javascript why do Date objects have both valueOf and getTime methods if they do the same?

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

like image 842
Attila Kun Avatar asked Mar 14 '12 20:03

Attila Kun


People also ask

What does Date getTime () do?

Javascript date getTime() method returns the numeric value corresponding to the time for the specified date according to universal time. The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00. You can use this method to help assign a date and time to another Date object.

What is the use of valueof method in JavaScript?

The valueof() method in JavaScript is used to return the primitive value of a number. This method is usually called internally by JavaScript and not explicitly in web code. Return Value: The valueof() method in JavaScript returns a number representing the primitive value of the specified Number object.

What does getTime return Date?

JavaScript Date getTime() getTime() returns the number of milliseconds since January 1, 1970 00:00:00.

What is primitive value of Date object?

Description. The valueOf() method returns the primitive value of a Date object as a number data type, the number of milliseconds since midnight 01 January, 1970 UTC. This method is functionally equivalent to the Date.


2 Answers

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 
like image 148
maerics Avatar answered Sep 21 '22 23:09

maerics


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.

like image 38
csikosjanos Avatar answered Sep 22 '22 23:09

csikosjanos