Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regarding JavaScript new Date() and Date.parse()

var exampleDate='23-12-2010 23:12:00';  

I want to convert above string into a date and have tried a couple things:

var date = new Date(exampleDate); //returns invalid Date
var date1 = Date.parse(exampleDate); //returns NAN

This code is running fine in IE and Opera, but date is returning me an invalid Date and date1 is returning NAN in Firefox. What should I do?

like image 395
Chitresh Avatar asked Dec 01 '10 04:12

Chitresh


People also ask

What does Date parse do in JavaScript?

The Date. parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).

What is new Date () in JavaScript?

The Date object is an inbuilt datatype of JavaScript language. 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.

Does new Date () return a string?

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. Returns a string representation of the current date and time.


3 Answers

The string in your example is not in any of the standard formats recognized by browsers. The ECMAScript specification requires browsers to be able to parse only one standard format:

The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ

This format includes date-only forms:

YYYY

YYYY-MM

YYYY-MM-DD

It also includes time-only forms with an optional time zone offset appended:

THH:mm

THH:mm:ss

THH:mm:ss.sss

Also included are “date-times” which may be any combination of the above.

If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognizable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.

So in your example, using 2010-12-23T23:12:00 is the only string guaranteed to work. In practice, most browsers also allow dates of the format DD Month YYYY or Month DD, YYYY, so strings like 23 Dec 2010 and Dec 23, 2010 could also work.

like image 98
casablanca Avatar answered Oct 06 '22 11:10

casablanca


Above format is only supported in IE and Chrome.

so try with another formats. following are some formats and there supporting browsers.

<script type="text/javascript">

//var dateString = "03/20/2008";  // mm/dd/yyyy [IE, FF]

 var dateString = "2008/03/20";  // yyyy/mm/dd [IE, FF]
// var dateString = "03-20-2008";  // mm-dd-yyyy [IE, Chrome]
// var dateString = "March 20, 2008";  // mmmm dd, yyyy [IE, FF]
// var dateString = "Mar 20, 2008";  // mmm dd, yyyy [IE, FF]

// Initalize the Date object by passing the date string variable
var myDate = new Date(dateString);
alert(myDate); 
</script>
like image 31
Romani Avatar answered Oct 06 '22 10:10

Romani


You could parse it manually with a regular expression then call the date constructor with the date elements, as such:

var parseDate = function(s) {
  var re = /^(\d\d)-(\d\d)-(\d{4}) (\d\d):(\d\d):(\d\d)$/;
  var m = re.exec(s);
  return m ? new Date(m[3], m[2]-1, m[1], m[4], m[5], m[6]) : null;
};
var dateStr = '23-12-2010 23:12:00';
parseDate(dateStr).toString(); //=> Thu Dec 23 2010 23:12:00 GMT-0800
like image 4
maerics Avatar answered Oct 06 '22 10:10

maerics