Difference between date (dateString) and new date (dateString)

I have code that is trying to parse a date string.

When I do alert(Date("2010-08-17 12:09:36")); It parses the date correctly and everything works fine, but I cannot call Date related methods 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 the new Date ()?

+43
javascript date
Aug 17 '10 at 18:30
source share
9 answers

Date ()

With this, you call a function called Date() . It takes no arguments and returns a string representing the current date and time.

new date ()

With this, you create 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) 

Thus, using 2010-08-17 12:09:36 as a parameter for the constructor is not allowed.

See w3schools .




EDIT: new Date(dateString) uses one of the following formats:

  • "October 13, 1975 11:13:00"
  • "October 13, 1975 11:13"
  • "October 13, 1975
+55
Aug 17 '10 at 18:41
source share

The following format works in all browsers:

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

So, so that the formatted date string yyyy-mm-dd hh:mm:ss fully compatible with the browser, you would need to replace the hyphens with a slash:

 var dateString = "2010-08-17 12:09:36"; new Date(dateString.replace(/-/g, "/")); 
+34
Dec 12 '13 at 9:57 on
source share

The difference is that (if I remember from the ECMA documentation) is that Date("xx") does not create (in a sense) a new date object (in fact, it is equivalent to calling ( new Date("xx").toString() )). Although new Date("xx") will actually create a new date object.

Additional Information:

Take a look at 15.9.2 from http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf

+3
Aug 17 '10 at 18:40
source share

I know this is old, but definitely the easiest to use

 var temp = new Date("2010-08-17T12:09:36"); 
+3
Apr 6 '14 at 20:18
source share

Correct ways to use Date: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date

In addition, the following code fragment shows how, with a single definition of the "Animal" function, it can be a) called directly and b) created by processing it as a constructor function

 function Animal(){ this.abc = 1; return 1234; } var x = new Animal(); var y = Animal(); console.log(x); //prints object containing property abc set to value 1 console.log(y); // prints 1234 
+1
Aug 17 '10 at 18:36
source share

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

Prior to ES5, there was no string format that browsers should support, although there is a number that is widely supported. However, browser support is unreliable, incompatible, for example. some of them allow you to use border values ​​and others, some support certain formats, while others do not, etc.

ES5 introduced support for some ISO 8601 formats, but the OP does not comply with the ISO 8601 standard, but not all browsers that support it, in any case.

The only reliable way is to use a small parsing function. The following example parses the format in the OP and also checks the values.

 /* Parse date string in format yyyy-mm-dd hh:mm:ss ** If string contains out of bounds values, an invalid date is returned ** ** @param {string} s - string to parse, eg "2010-08-17 12:09:36" ** treated as "local" date and time ** @returns {Date} - Date instance created from parsed string */ function parseDateString(s) { var b = s.split(/\D/); var d = new Date(b[0], --b[1], b[2], b[3], b[4], b[5]); return d && d.getMonth() == b[1] && d.getHours() == b[3] && d.getMinutes() == b[4]? d : new Date(NaN); } document.write( parseDateString('2010-08-17 12:09:36') + '<br>' + // Valid values parseDateString('2010-08-45 12:09:36') // Out of bounds date ); 
+1
Feb 17 '16 at 6:18
source share

Error getting invalid date. Rather, the temp value is "Invalid date."

Is your date string valid? If you are using Firefox, check Date.parse

In Firefox javascript console:

 >>> Date.parse("2010-08-17 12:09:36"); NaN >>> Date.parse("Aug 9, 1995") 807944400000 

I would try a different date string format.

Zebi, are you using Internet Explorer?

0
Aug 17 '10 at 18:44
source share

I had the same problem using an API call that answered in ISO 8601 format. Working in Chrome it worked: `

 // date variable from an api all in ISO 8601 format yyyy-mm-dd hh:mm:ss var date = oDate['events']['event'][0]['start_time']; var eventDate = new Date(); var outputDate = eventDate.toDateString(); 

`

but this did not work with firefox.

The above answer helped me format it correctly for firefox:

  // date variable from an api all in ISO 8601 format yyyy-mm-dd hh:mm:ss var date = oDate['events']['event'][0]['start_time']; var eventDate = new Date(date.replace(/-/g,"/"); var outputDate = eventDate.toDateString(); 
0
Dec 30 '14 at 20:57
source share

I recently came across this and it was a useful post. I took Topera above another step, and this works for me in both chrome and firefox:

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

the internal call to Date() returns a string that new Date() can parse.

-2
Nov 22 '13 at 17:15
source share



All Articles