Jonathan Livni
Jonathan Livni

Reputation: 107092

Date vs new Date in JavaScript

new Date() takes an ordinal and returns a Date object.
What does Date() do, and how come it gives a different time?

>>> new Date(1329429600000)
Date {Fri Feb 17 2012 00:00:00 GMT+0200 (القدس Standard Time)}
>>> Date(1329429600000)
"Tue Mar 06 2012 15:29:58 GMT+0200 (Jerusalem Standard Time)"

Upvotes: 97

Views: 33483

Answers (8)

pollx
pollx

Reputation: 699

It's 2017 and I had the same question in mind. What I found as an answer after some reading:

"The simplest way to perform an explicit type conversion is to use the Boolean() , Number() , String() , or Object() functions. We’ve already seen these functions as constructors for wrapper objects. When invoked without the new operator, however, they work as conversion functions and perform type conversions.."

"The built-in classes of core JavaScript attempt valueOf() conversion before toString() conversion, except for the Date class, which performs toString() conversion."

So Date() invoked without the new keyword performs a type conversion. And since Date is an object and an object-to-primitive should happen, date objects by default call toString() (though Date has meaningful valueOf() method as well).

Found that on the "JavaScript: The Definitive Guide" book. Leaving it here for the future generations who have just started learning JS :)

Upvotes: 7

Abdennour TOUMI
Abdennour TOUMI

Reputation: 93183

Date class can be called as constructor or as method to have a built-in code like :

function Date(args){
   if (this.constructor == Date){
        // if you call : new Date(args)
    }else{
        // if you call as method : Date()
      return new Date()
   }

}

Thus , if you called it like method , it re-call the constructor to return the current date&time .

Upvotes: 3

S.Munkhey
S.Munkhey

Reputation: 1

Calling a constructor as a Function is plain wrong it'll do (probably) unexpected things with your app scope and before very long you'll be the focus of attention in a group bug fixing session.

Create a Date Object as intended by the designers of the spec, don't code to the workarounds implemented as safeguards by engineers that think JS programmers are stupid. (worked in the lab, was in the next chair during the conversation, dealt with it and moved on)

If you are madly against new you can try object.create but at time of writing it's slower and unless you are planning to implement polymorphic inheritance then it is extra effort for less reward.

Upvotes: -3

nkron
nkron

Reputation: 19791

Check out JavaScript Date for a quick API reference and code test bed. You can see the Date() function called without new does not take any parameters and always returns a string representation of the current date/time. If you modify the sample to be:

console.log(Date());
console.log(Date(1329429600000));

You'll find the results for both are the same (because JavaScript ignores extra arguments passed to functions):

Wed Apr 11 2012 09:58:11 GMT-0700 (PDT)
Wed Apr 11 2012 09:58:11 GMT-0700 (PDT)

Upvotes: 12

user1252065
user1252065

Reputation:

new Date creates a new Date object that you can modify or initialize with a different date while Date returns a string of the current date/time, ignoring its arguments.

Upvotes: 31

mav
mav

Reputation: 1248

Date lets you create objects that represent date/time. It's NOT meant to be called like a function. You can get more information here: Date - MDN

Upvotes: 2

PraveenVenu
PraveenVenu

Reputation: 8337

new Date() returns the date based on the input parameter and Date() returns todays date on the browser.

Upvotes: 3

pimvdb
pimvdb

Reputation: 154828

From the specs:

When Date is called as a function rather than as a constructor, it returns a String representing the current time (UTC).

and:

When Date is called as part of a new expression, it is a constructor: it initialises the newly created object.

So, new Date(...) returns an object such that obj instanceof Date is true, whereas Date(...) basically returns the same as new Date().toString().

Upvotes: 163

Related Questions