views:

1151

answers:

3

I'm trying to build a little calendar in javascript, and I've got my dates working great in Firefox and chrome, but in IE, the date functions are returning NAN.

Here is the function

function buildWeek(dateText){
            alert(dateText);
            var headerDates='';
            var newDate = new Date(dateText);
            var startSched=formatDates(newDate);

            newDate.setDate(newDate.getDate()-1);

            for(var d=0;d';
            newDate.setDate(newDate.getDate()+5);
            newDate.setDate(newDate.getDate()+1);
            var theseDates=formatDates(newDate);
            headerDates+=''+theseDates[1][0]+'';
            if(d==6){
            newDate.setDate(newDate.getDate()+2);
            theseDates=formatDates(newDate);
            headerDates+='>';                }
                            }                       
        jQuery('div#headerDates').html(''+headerDates+'');
                                }

The datetext is the monday of the current week which is actually set in php in the format of 'm, d, Y'.

Eg (02, 01, 2010);

A: 

The Date constructor in JavaScript needs a string in one of the date formats supported by the parse() method.

Apparently, the format you are specifying isn't supported in IE, so you'll need to either change the PHP code, or parse the string manually in JavaScript.

richardtallent
That much is pretty obvious, but it would be good to know what supported formats might be...
StaxMan
+1  A: 

Send the date text and format in which u r sending the datetext in the below method it will parse and return as date and this is independent of browser

function cal_parse_internal(val, format) {
val = val + "";
format = format + "";
var i_val = 0;
var i_format = 0;
var x, y;
var now = new Date(dbSysCurrentDate);
var year = now.getYear();
var month = now.getMonth() + 1;
var date = now.getDate();

while (i_format < format.length) {
    // Get next token from format string
    var c = format.charAt(i_format);
    var token = "";
    while ((format.charAt(i_format) == c) && (i_format < format.length)) {
        token += format.charAt(i_format++);
    }
    // Extract contents of value based on format token
    if (token == "yyyy" || token == "yy" || token == "y") {
        if (token == "yyyy") { x = 4; y = 4; }
        if (token == "yy")   { x = 2; y = 2; }
        if (token == "y")    { x = 2; y = 4; }
        year = _getInt(val, i_val, x, y);
        if (year == null) { return 0; }
        i_val += year.length;
        if (year.length == 2) {
            if (year > 70) {
                year = 1900 + (year - 0);
            } else {
                year = 2000 + (year - 0);
            }
        }
    } else if (token == "MMMM") {
        month = 0;
        for (var i = 0; i < MONTHS_LONG.length; i++) {
            var month_name = MONTHS_LONG[i];
            if (val.substring(i_val, i_val + month_name.length) == month_name) {
                month = i + 1;
                i_val += month_name.length;
                break;
            }
        }
        if (month < 1 || month > 12) { return 0; }
    } else if (token == "MMM") {
        month = 0;
        for (var i = 0; i < MONTHS_SHORT.length; i++) {
            var month_name = MONTHS_SHORT[i];
            if (val.substring(i_val, i_val + month_name.length) == month_name) {
                month = i + 1;
                i_val += month_name.length;
                break;
            }
        }
        if (month < 1 || month > 12) { return 0; }
    } else if (token == "MM" || token == "M") {     
        month = _getInt(val, i_val, token.length, 2);
        if (month == null || month < 1 || month > 12) { return 0; }
        i_val += month.length;
    } else if (token == "dd" || token == "d") {
        date = _getInt(val, i_val, token.length, 2);
        if (date == null || date < 1 || date > 31) { return 0; }
        i_val += date.length;
    } else {
        if (val.substring(i_val, i_val+token.length) != token) {return 0;}
        else {i_val += token.length;}
    }
}

// If there are any trailing characters left in the value, it doesn't match
if (i_val != val.length) { return 0; }

// Is date valid for month?
if (month == 2) {
    // Check for leap year
    if ((year%4 == 0 && year%100 != 0) || (year%400 == 0)) { // leap year
        if (date > 29) { return false; }
    } else {
        if (date > 28) { return false; }
    }
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
    if (date > 30) { return false; }
}
return new Date(year, month - 1, date);

}

valli
Thanks Valli. I went with garrett's answer below, as the function he linked to was smaller, and worked with the date format I regularly use.
pedalpete
+4  A: 

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.

Garrett
Thanks Garrett. That function you provided in the link is the best far more concise than anything else I've been able to find, and it works with the date format that I use through the other 99% of the app! Much more consistent than what I was using.
pedalpete
This is, alas, not good, since many structured formats just use W3C date/time formats (ISO-8601 with full specs or so). So while timestamps are better in many ways (simpler, more efficient, work with all browsers), it is often required that standard date/time data is parser from javascript.I wonder if jQuery or such would have better parsing methods.
StaxMan