开发者

Converting a string into a Date via javascript

开发者 https://www.devze.com 2023-01-20 00:23 出处:网络
I have a string I need converted in开发者_StackOverflow中文版to a date: 2010-10-14T09:00:00.0000000

I have a string I need converted in开发者_StackOverflow中文版to 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...


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;
            });
0

精彩评论

暂无评论...
验证码 换一张
取 消