Anders
Anders

Reputation: 12726

Convert string to date in jQuery and Internet Explorer?

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?

Upvotes: 3

Views: 27321

Answers (5)

Markus Knappen Johansson
Markus Knappen Johansson

Reputation: 1630

I use moment like this:

new Date(moment(item.ToDate));

Works with Swedish dates as well '2013-01-05':

new Date(moment('2013-01-05'));

Upvotes: 0

Neil
Neil

Reputation: 11

I haven't tested this, but how about:

var followupdate = new Date(jQuery(this).text().trim().toString());

The "toString()" should force it to be interpreted as a string; the Date object should accept the string as valid input, and it might prevent IE from throwing up on it.

Upvotes: 1

Matt K
Matt K

Reputation: 7337

This question helped me figure out a solution to a problem I was having converting dates. I found a way to convert the date without using separate scripts or testing for browser type.

The code below accepts a date in the format 2011-01-01 (year, month, day).

function convertDate(stringdate)
{
    // Internet Explorer does not like dashes in dates when converting, 
    // so lets use a regular expression to get the year, month, and day 
    var DateRegex = /([^-]*)-([^-]*)-([^-]*)/;
    var DateRegexResult = stringdate.match(DateRegex);
    var DateResult;
    var StringDateResult = "";

    // try creating a new date in a format that both Firefox and Internet Explorer understand
    try
    {
        DateResult = new Date(DateRegexResult[2]+"/"+DateRegexResult[3]+"/"+DateRegexResult[1]);
    } 
    // if there is an error, catch it and try to set the date result using a simple conversion
    catch(err) 
    { 
        DateResult = new Date(stringdate); 
    }

    // format the date properly for viewing
    StringDateResult = (DateResult.getMonth()+1)+"/"+(DateResult.getDate()+1)+"/"+(DateResult.getFullYear());

    return StringDateResult;
}

Hope that helps!

Upvotes: 6

Anders
Anders

Reputation: 12726

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.

Upvotes: 1

castis
castis

Reputation: 8223

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()

?

Upvotes: 1

Related Questions