views:

44

answers:

2

I want to convert a date string to a date object in jQuery, and the code below works fine for Chrome and Firefox, but not in Internet Explorer:

<script type="text/javascript" charset="utf-8">
//Validate if the followup date is now or passed:
    jQuery.noConflict();
    var now = new Date();
    jQuery(".FollowUpDate").each(function () {
        if (jQuery(this).text().trim() != "") {
            var followupDate = new Date(jQuery(this).text().trim()); //Here's the problem
            alert(followupDate);
            if (followupDate <= now) {
                jQuery(this).removeClass('current');
                jQuery(this).addClass('late');
            }
            else {
                jQuery(this).removeClass('late');
                jQuery(this).addClass('current');
            }
        }
    });
</script>

The alert is only there for testing, and in Chrome and Firefox it returns a date object, but in IE I get NaN.

What's wrong, and how can I do this conversion so that it works in IE as well?

A: 

If its a string that looks like a date, use this.

var followupDate = new Date(Date.Parse(jQuery(this).text().trim()));

I guess a question I should have asked is, what is the output of

jQuery(this).text().trim()

?

castis
Unfortunately, that gave me the exact same result (NaN). The output of jQuery(this).text().trim() is the content of a table cell, which is a date string in Swedish date format ("2010-10-30"). Firefox and Chrome has no problem with this and immediately turns it into a date object, but IE doesn't seem to recognize it or something...
Anders Svensson
A: 

I figured it out: IE apparently did not accept the Swedish date format, so I did a string replace to a format it did accept:

var followupDate = new Date(datestring.replace('-', '/'));

Unfortunately this format wasn't accepted by Firefox, so I had to keep the original code for Chrome and Firefox, and then use a separate script for IE with conditional comments.

Anders Svensson