Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date.getTime() v.s. Date.now()

Tags:

javascript

I noticed that now() can only be called by the Date object. getTime() can only be called by an instance of date.

var dd1 = new Date();

//console.log(dd1.now()); //Throws error -> TypeError: Object Mon Aug 19 2013 16:28:03 GMT-0400 (Eastern Daylight Time) has no method 'now'
console.log(dd1.getTime());

console.log(Date.now());
//console.log(Date.getTime()); //Throws error ->TypeError: Object function Date() { [native code] } has no method 'getTime'

Is there a formal name for this difference? Is this the difference between "static" and "non-static." When I create a new instance of Date, shouldn't all methods be inherited?

like image 531
user2316667 Avatar asked Aug 19 '13 20:08

user2316667


People also ask

What is the difference between Date now and new Date?

new Date() - creates a Date object representing the current date/time. Date. now() - returns the number of milliseconds since midnight 01 January, 1970 UTC.

What does Date getTime () do?

getTime() The getTime() method returns the number of milliseconds since the ECMAScript epoch. You can use this method to help assign a date and time to another Date object. This method is functionally equivalent to the valueOf() method.

Is new Date () getTime () UTC?

Use the getTime() method to get a UTC timestamp, e.g. new Date(). getTime() . The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation. Calling the method from any time zone returns the same UTC timestamp.

What does Date now () mean?

The date. now() method is used to return the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. Since now() is a static method of Date, it will always be used as Date.


2 Answers

It's the difference between properties of the constructor object and properties of the constructor object's prototype. The "now" property is a property of the Date constructor itself, and not a property of Date.prototype. It's the opposite situation for "getTime".

Semantically it makes sense: the concept of "now" is independent of any particular date instance. The "getTime" method is intended to report on the timestamp for the date actually represented by a particular date instance.

If you're defining your own constructors, you can create "class methods" (I personally would hesitate to call them that, but whatever) like this:

function MyConstructor() {
  // ...
}

MyConstructor.someMethod = function() {
  // ...
}

Then MyConstructor.someMethod() calls that function independently of any particular instance of your class.

like image 142
Pointy Avatar answered Sep 21 '22 21:09

Pointy


I don't know if this answers your question but I think these two methods do different things.

Date.now() gets "The number of milliseconds between midnight, January 1, 1970, and the current date and time."

Whereas, dateObj.getTime() "Returns the number of milliseconds between midnight, January 1, 1970 and the time value in the Date object."

dateObj.getTime() requires you to use an object reference, because it uses that to get the difference "between midnight, January 1, 1970 and the time value in the Date object."

You don't need to reference an object with the Date.now() method because it doesn't use any object, it simply gets the "number of milliseconds between midnight, January 1, 1970, and the current date and time."

Ref. Date.Now()

Ref. dateObj.getTime()

like image 27
user1477388 Avatar answered Sep 21 '22 21:09

user1477388