views:

27

answers:

1

I have a string I need converted into a date:

2010-10-14T09:00:00.0000000

In FF and Crome I can do var date = new Date("2010-10-14T09:00:00.0000000") and everything works. That code in IE, Safari and Opera gives be a NaN. How can I get that string into a date in a x-browser manner, preferably without manually parsing the string into its individual parts.

I need both the date and time parts to be converted...

+1  A: 

Not pretty, but: http://delete.me.uk/2005/03/iso8601.html or use json2.js and:

    myData = JSON.parse(text, function (key, value) {
                var a;
                if (typeof value === 'string') {
                    a =
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
                    if (a) {
                        return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
                            +a[5], +a[6]));
                    }
                }
                return value;
            });
Detect
The code in the Link worked great.
Justin808