首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >不同浏览器中日期值的JSON.stringify差异

不同浏览器中日期值的JSON.stringify差异
EN

Stack Overflow用户
提问于 2010-02-08 06:04:52
回答 4查看 9.1K关注 0票数 17

我在一个HTML页面中有以下代码:

alert(JSON.stringify(new Date()));

我在我的页面中包含了最新的json2.js (2009-09-29版本),以支持没有JSON.stringify()的旧浏览器。我还包含了jquery-1.3.2.js。我相信具有原生JSON支持的较新浏览器,它只是传递到原生JSON特性。

下面是我在不同浏览器中得到的结果:

IE 8 on Windows XP: "2010-02-07T21:39:32Z"
Chrome 4.0 on Windows XP: "2010-02-07T21:39:59Z"
Firefox 3.0 of Windows XP: "2010-02-07T21:40:41Z"
Chrome 4.0 on Ubuntu linux:  "2010-02-07T21:41:49Z"
Firefox 3.0 on Ubuntu linux:  "2010-02-07T21:42:44Z"
Chrome 4.0 on Mac OSX: "2010-02-07T21:43:56Z"
Safari on Mac OSX: "2010-02-07T21:45:21Z"
Firefox 3.5 on Mac OSX: "2010-02-07T21:44:10.101Z"

注意到最后一个了吗?它包含毫秒,其他任何一个都不包含。我没有在任何其他系统上安装FF3.5,但我假设它们会有相同的结果。

有什么我可以做的,使所有平台上的所有日期都是相同的吗?我的后端REST服务可以配置一个格式字符串来反序列化JSON日期,但它不能支持多种格式,只支持一种格式。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-02-08 07:53:24

我添加了下面的javascript就可以工作了:

// Added to make dates format to ISO8601
Date.prototype.toJSON = function (key) {
    function f(n) {
        // Format integers to have at least two digits.
        return n < 10 ? '0' + n : n;
    }

    return this.getUTCFullYear()   + '-' +
         f(this.getUTCMonth() + 1) + '-' +
         f(this.getUTCDate())      + 'T' +
         f(this.getUTCHours())     + ':' +
         f(this.getUTCMinutes())   + ':' +
         f(this.getUTCSeconds())   + '.' +
         f(this.getUTCMilliseconds())   + 'Z';
};

我敢肯定这可能会减慢序列化的速度,但它似乎使不同浏览器之间的事情保持一致。

票数 9
EN

Stack Overflow用户

发布于 2010-02-08 07:53:19

您还可以对json2.js进行一些调整,使其始终使用自己的Date.prototype.toJSON,而不是可能的本机a。这里我取消了两行的注释,它可以正常工作:

// if (typeof Date.prototype.toJSON !== 'function') {

    Date.prototype.toJSON = function (key) {

        return isFinite(this.valueOf()) ?
               this.getUTCFullYear()   + '-' +
             f(this.getUTCMonth() + 1) + '-' +
             f(this.getUTCDate())      + 'T' +
             f(this.getUTCHours())     + ':' +
             f(this.getUTCMinutes())   + ':' +
             f(this.getUTCSeconds())   + 'Z' : null;
    };

    String.prototype.toJSON =
    Number.prototype.toJSON =
    Boolean.prototype.toJSON = function (key) {
        return this.valueOf();
    };
// }
票数 4
EN

Stack Overflow用户

发布于 2010-02-08 11:48:06

//您可能需要考虑增强服务器,以识别任何有效的ISO 8601时间格式:

'2010-02-08T03:37:34.327Z‘

'2010-02-08T03:38:06Z‘

'2010-02-08T03:38+01:00‘

'2010-02-08T03:34:18-05:00‘

'2010-02-08T03:34Z‘

'2010-02-08‘

这是一种将任何iso字符串转换为javascript date对象的方法。它可以在服务器上使用,只需做一点翻译:

Date.from_iso= function(s){
    var D, M= [], hm, min= 0, d2,
    Rx=  /([\d:]+)(\.\d+)?(Z|(([+\-])(\d\d):(\d\d))?)?$/;
    D= s.substring(0, 10).split('-');
    if(s.length> 11){
        M= s.substring(11).match(Rx) || [];
        if(M[1]) D= D.concat(M[1].split(':'));
        if(M[2]) D.push(Math.round(M[2]*1000));// msec
    }
    for(var i= 0, L= D.length; i<L; i++){
        D[i]= parseInt(D[i], 10);
    }
    D[1]-= 1;
    while(D.length< 6) D.push(0);
    if(M[4]){
        min= parseInt(M[6])*60+ parseInt(M[7], 10);// timezone not UTC
        if(M[5]== '+') min*= -1;
    }
    try{
        d2= Date.fromUTCArray(D);
        if(min) d2.setUTCMinutes(d2.getUTCMinutes()+ min);
    }
    catch(er){
        // bad input
    }
    return d2;
}
Date.fromUTCArray= function(A){
    var D= new Date;
    while(A.length < 7) A.push(0);
    var T= A.splice(3, A.length);
    D.setUTCFullYear.apply(D, A);
    D.setUTCHours.apply(D, T);
    return D;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2218564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档