Date Constructor Returns Nan in Ie, But Works in Firefox and Chrome

Date constructor returns NaN in IE, but works in Firefox and Chrome

The Date constructor accepts any value. If the primitive [[value]] of the argument is number, then the Date that is created has that value. If primitive [[value]] is String, then the specification only guarantees that the Date constructor and the parse method are capable of parsing the result of Date.prototype.toString and Date.prototype.toUTCString()

A reliable way to set a Date is to construct one and use the setFullYear and setTime methods.

An example of that appears here:
http://jibbering.com/faq/#parseDate

ECMA-262 r3 does not define any date formats. Passing string values to the Date constructor or Date.parse has implementation-dependent outcome. It is best avoided.


Edit:
The entry from comp.lang.javascript FAQ is:
An Extended ISO 8601 local date format YYYY-MM-DD can be parsed to a Date with the following:-

/**Parses string formatted as YYYY-MM-DD to a Date object.
* If the supplied string does not match the format, an
* invalid Date (value NaN) is returned.
* @param {string} dateStringInRange format YYYY-MM-DD, with year in
* range of 0000-9999, inclusive.
* @return {Date} Date object representing the string.
*/

function parseISO8601(dateStringInRange) {
var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
date = new Date(NaN), month,
parts = isoExp.exec(dateStringInRange);

if(parts) {
month = +parts[2];
date.setFullYear(parts[1], month - 1, parts[3]);
if(month != date.getMonth() + 1) {
date.setTime(NaN);
}
}
return date;
}

Date difference works in Firefox and IE but return NaN in Chrome

Use momentjs if your input is 'DD/MM/YYYY'

Date constructor returns NaN in IE, but works in Firefox and Chrome

The Date constructor accepts any value. If the primitive [[value]] of the argument is number, then the Date that is created has that value. If primitive [[value]] is String, then the specification only guarantees that the Date constructor and the parse method are capable of parsing the result of Date.prototype.toString and Date.prototype.toUTCString()

A reliable way to set a Date is to construct one and use the setFullYear and setTime methods.

An example of that appears here:
http://jibbering.com/faq/#parseDate

ECMA-262 r3 does not define any date formats. Passing string values to the Date constructor or Date.parse has implementation-dependent outcome. It is best avoided.


Edit:
The entry from comp.lang.javascript FAQ is:
An Extended ISO 8601 local date format YYYY-MM-DD can be parsed to a Date with the following:-

/**Parses string formatted as YYYY-MM-DD to a Date object.
* If the supplied string does not match the format, an
* invalid Date (value NaN) is returned.
* @param {string} dateStringInRange format YYYY-MM-DD, with year in
* range of 0000-9999, inclusive.
* @return {Date} Date object representing the string.
*/

function parseISO8601(dateStringInRange) {
var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
date = new Date(NaN), month,
parts = isoExp.exec(dateStringInRange);

if(parts) {
month = +parts[2];
date.setFullYear(parts[1], month - 1, parts[3]);
if(month != date.getMonth() + 1) {
date.setTime(NaN);
}
}
return date;
}

Time show Nan:Nan in Firefox, Chrome works perfect

If you don't want to change how the date is stored internally, and don't mind including an external library, moment.js will parse these strings for you correctly if you provide the format. See the docs for parsing (http://momentjs.com/docs/#/parsing/string-format/) and formatting (http://momentjs.com/docs/#/displaying/format/)

var d = new moment(item.starttime, "YYYY-MM-DD HH:mm:ss");
// To get the date as 'Friday 25th January 2013'
var datestring = d.format("dddd Do MMMM YYYY");
// To get the time as '09:30'
var timestring = d.format("HH:mm");

Date Returning NaN In Mozilla and IE With Javascript

From Date on MDN:

new Date(dateString);

String value representing a date. The string should be in a format
recognized by the Date.parse() method (IETF-compliant RFC 2822
timestamps and also a version of ISO8601).

From Date.parse() on MDN:

A string representing an RFC2822 or ISO 8601 date (other formats may
be used, but results may be unexpected).

(Bold is mine)

Example of RFC2822 formatted date:

Mon, 25 Dec 1995 13:30:00 GMT

Example of ISO formats:

2011-10-10
2011-10-10T14:48:00

Your date is not in one of these two formats so browser behaviour is undefined.

Try formatting your string as per standard, or parsing it and using the constructor:

new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);

Function getDate doesn't work in FireFox and IE

    function dateConverter(dateFromXml){
function format(x){
return (x < 10) ? '0' + x : x;
}
var modifiedDate = date.split('-').join('/'),
d = new Date(modifiedDate),
convertedDate = [format(d.getDate()), format(d.getMonth()), d.getFullYear()].join('/');

return convertedDate + time;
}

Hi guys,
I allready solved my own problem, thanks to all who left a comment.
Now i will explane my code.
What i did in FireFox was: i printed out my result on 'd' and on 'dateFromXml'. I saw that 'd' was an invalid date in firefox and 'dateFromXml' return the date yyyy-mm-dd hh:mm:ss. so i started researching and i found a very helpfull webpage (http://dygraphs.com/date-formats.html) On this webpage you will find all kind of formats for dates. And on this webpage i saw that my date that i got from the XML file was in a invalid format because it is written with '-' instead of '/' so i split the date on '-' and joined it with '/'. thats all... :)



Related Topics



Leave a reply



Submit