Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date vs new Date in JavaScript

Tags:

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)"
like image 916
Jonathan Livni Avatar asked Mar 06 '12 13:03

Jonathan Livni


People also ask

What is the difference between date and new date in JavaScript?

Date() returns an implementation dependent string representing the current date and time. new Date() returns a Date object that represents the current date and time. ;-) Yeah, I know.

What is new Date () in JavaScript?

It is used to work with dates and times. The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970.

Is new Date () a string?

Calling the Date() function (without the new keyword) returns a string representation of the current date and time, exactly as new Date().

Does new Date () Return current date?

Use new Date() to generate a new Date object containing the current date and time. This will give you today's date in the format of mm/dd/yyyy.


1 Answers

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

like image 60
pimvdb Avatar answered Oct 09 '22 03:10

pimvdb