Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Date(dateString) and new Date(dateString)

I have some code that tries to parse a date string.

When I do alert(Date("2010-08-17 12:09:36")); It properly parses the date and everything works fine but I can't call the methods associated with Date, like getMonth().

When I try:

var temp = new Date("2010-08-17 12:09:36"); alert(temp); 

I get an "invalid date" error.

Any ideas on how to parse "2010-08-17 12:09:36" with new Date()?

like image 668
FinDev Avatar asked Aug 17 '10 18:08

FinDev


People also ask

What is the difference between date and new date?

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.

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 does new date () mean?

An object that represents a date and time. Internally the time is stored as the number of milliseconds since 01 January, 1970 UTC. Use new Date() to get a Date for the current time or Date. now() to get the current time in milliseconds since 01 January, 1970 UTC. Spec.

What is new date return?

"The expression new Date() returns the current time in internal format, as an object containing the number of milliseconds elapsed since the start of 1970 in UTC.

What is the difference between New date and new date?

new Date () creates a new date object with the current date and time: Date objects are static. The computer time is ticking, but date objects are not. new Date ( year, month, ...) new Date ( year, month, ...) creates a new date object with a specified date and time.

What is a date string in JavaScript?

new Date (dateString) creates a new date object from a date string: Date strings are described in the next chapter. JavaScript stores dates as number of milliseconds since January 01, 1970, 00:00:00 UTC (Universal Time Coordinated). Zero time is January 01, 1970 00:00:00 UTC.

What is the format of the date string in New date?

EDIT: new Date (dateString) uses one of these formats: Show activity on this post. The following format works in all browsers: So, to make a yyyy-mm-dd hh:mm:ss formatted date string fully browser compatible you would have to replace dashes with slashes:

Is it possible to parse date string in MDN?

Note that MDN strongly discourages the use of new Date (dateString) or Date.parse (dateString): Note: Parsing of date strings with the Date constructor (and Date.parse (), which works the same way) is strongly discouraged due to browser differences and inconsistencies.


2 Answers

Date()

With this you call a function called Date(). It doesn't accept any arguments and returns a string representing the current date and time.

new Date()

With this you're creating a new instance of Date.

You can use only the following constructors:

new Date() // current date and time new Date(milliseconds) //milliseconds since 1970/01/01 new Date(dateString) new Date(year, month, day, hours, minutes, seconds, milliseconds) 

So, use 2010-08-17 12:09:36 as parameter to constructor is not allowed.

See w3schools.


EDIT: new Date(dateString) uses one of these formats:

  • "October 13, 1975 11:13:00"
  • "October 13, 1975 11:13"
  • "October 13, 1975"
like image 64
Topera Avatar answered Sep 29 '22 13:09

Topera


The following format works in all browsers:

new Date("2010/08/17 12:09:36"); 

So, to make a yyyy-mm-dd hh:mm:ss formatted date string fully browser compatible you would have to replace dashes with slashes:

var dateString = "2010-08-17 12:09:36"; new Date(dateString.replace(/-/g, "/")); 
like image 38
Daghall Avatar answered Sep 29 '22 14:09

Daghall