Date vs new Date in JavaScript

Javascript

Javascript Problem Overview


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)"

Javascript Solutions


Solution 1 - Javascript

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().

Solution 2 - Javascript

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.

Solution 3 - Javascript

Check out http://www.javascripture.com/Date">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)

Solution 4 - Javascript

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 :)

Solution 5 - Javascript

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

Solution 6 - Javascript

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 .

Solution 7 - Javascript

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

Solution 8 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonathan LivniView Question on Stackoverflow
Solution 1 - JavascriptpimvdbView Answer on Stackoverflow
Solution 2 - Javascriptuser1252065View Answer on Stackoverflow
Solution 3 - JavascriptnkronView Answer on Stackoverflow
Solution 4 - JavascriptpollxView Answer on Stackoverflow
Solution 5 - JavascriptPraveenVenuView Answer on Stackoverflow
Solution 6 - JavascriptAbdennour TOUMIView Answer on Stackoverflow
Solution 7 - JavascriptmavView Answer on Stackoverflow
Solution 8 - JavascriptS.MunkheyView Answer on Stackoverflow