I want to parse date in my page to Javascript\'s Date
.
So I have this in my page
01-07-2012 01:04 PM
try this:
var tagText = $(this).html();
tagText = tagText.replace(/-/g, '/');
var givenDate = new Date(tagText);
alert(givenDate);
What I must change to make it work with all major browsers?
Write it in milliseconds.
If you really want full cross-browser support for any date format, you should take a look at moment.js. It allows you to be explicit about the input format. For example:
var m = moment('01-07-2012 01:04 PM', 'DD-MM-YYYY hh:mm a');
As explained in the documentation the string you are passing to the constructor of the Date object should be:
String value representing a date. The string should be in a format recognized by the parse method (IETF-compliant RFC 1123 timestamps).
Basically it should represent an RFC822 or ISO 8601 date.