82

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)"
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359

8 Answers8

148

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

pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 2
    any pseudo-code of how Date() function is implemented so that it can work as both a constructor and a regular function? thanks – Benny Oct 07 '14 at 16:57
  • 4
    @Benny observe the following: `function Test(){ var private = "private value"; this.public = "public value"; return "return value"; }` Calling `new Test()` creates an object with private and public values. Calling `Test()` returns a string. – musicin3d Nov 18 '14 at 20:22
  • @Benny Here's how it might be implemented: `function Date(/* ... */) { if (!(this instanceof Date)) { return new Date().toString(); } else { /* we got `new`, so do the constructor logic */ }` – Jo Liss Oct 20 '15 at 18:09
28

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.

11

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)
nkron
  • 19,086
  • 3
  • 39
  • 27
7

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

pollx
  • 643
  • 9
  • 21
3

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 .

Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
3

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

PraveenVenu
  • 8,217
  • 4
  • 30
  • 39
2

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

mav
  • 1,230
  • 1
  • 15
  • 23
-3

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.