JavaScript 日期 ISO8601

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6228302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:50:54  来源:igfitidea点击:

JavaScript Date ISO8601

javascriptdate

提问by Luc

What is the best way to get a Javascript Date object from a string like the following one:

从如下字符串中获取 Javascript Date 对象的最佳方法是什么:

2011-06-02T09:34:29+02:00 ?

I have trouble with the time zone part (obviously).

我在时区部分有问题(显然)。

回答by Jason McCreary

var myDate = new Date('2011-06-02T09:34:29+02:00');
alert(myDate);

回答by kennebec

IE 8 and below, and older versions of the other browsers do not implement the ISO Date format. A problem is that some of the browsers do return a date, instead of NaN, just not the correct one.

IE 8 及以下版本以及其他浏览器的旧版本不实现 ISO 日期格式。一个问题是一些浏览器确实返回一个日期,而不是 NaN,只是不正确。

You can write your own method, if you want to support them. The time zone is the tricky bit.

如果您想支持它们,您可以编写自己的方法。时区是一个棘手的问题。

This example will run once and set a Date.fromISO method- if the native method is supported it will use it.

此示例将运行一次并设置 Date.fromISO 方法 - 如果支持本机方法,它将使用它。

(function(){
var D= new Date('2011-06-02T09:34:29+02:00');
if(isNaN(D) || D.getUTCMonth()!== 5 || D.getUTCDate()!== 2 ||
D.getUTCHours()!== 7 || D.getUTCMinutes()!== 34){
    Date.fromISO= function(s){
        var day, tz,
        rx=/^(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d\d):(\d\d))?$/,
        p= rx.exec(s) || [];
        if(p[1]){
            day= p[1].split(/\D/);
            for(var i= 0, L= day.length; i<L; i++){
                day[i]= parseInt(day[i], 10) || 0;
            }
            day[1]-= 1;
            day= new Date(Date.UTC.apply(Date, day));
            if(!day.getDate()) return NaN;
            if(p[5]){
                tz= (parseInt(p[5], 10)*60);
                if(p[6]) tz+= parseInt(p[6], 10);
                if(p[4]== '+') tz*= -1;
                if(tz) day.setUTCMinutes(day.getUTCMinutes()+ tz);
            }
            return day;
        }
        return NaN;
    }
    // remove test:
    alert('ISO Date Not correctly implemented');
}
else{
    Date.fromISO= function(s){
        return new Date(s);
    }
    // remove test:
    alert('ISO Date implemented');
}
})()


// remove test
var D=Date.fromISO('2011-06-02T09:34:29+02:00');
alert(D.toUTCString())

回答by Eli

If your string is an ISO8601 string, you can just pass it into the Date constructor and get a Date object back out:

如果您的字符串是 ISO8601 字符串,您可以将它传递给 Date 构造函数并返回一个 Date 对象:

var date = new Date('2011-06-02T09:34:29+02:00');

According to http://dev.enekoalonso.com/2010/09/21/date-from-iso-8601-string/though, this might have issues in IE and other platforms. It recommends you do something like this for compatibility:

不过,根据http://dev.enekoalonso.com/2010/09/21/date-from-iso-8601-string/,这可能在 IE 和其他平台上存在问题。为了兼容性,它建议您执行以下操作:

function dateFromISO8601(isostr) {
    var parts = isostr.match(/\d+/g);
    return new Date(parts[0], parts[1] – 1, parts[2], parts[3], parts[4], parts[5]);
}

var myDate = dateFromISO8601("2011-06-02T09:34:29+02:00");
console.log(myDate);