【问题标题】:Momentjs, compare utc datesMomentjs,比较UTC日期
【发布时间】:2014-04-14 15:40:31
【问题描述】:

好的,我循环遍历来自我的服务器的时间数组,格式如下:

2014-04-14T13:00:00Z
2014-04-14T13:15:00Z
2014-04-14T13:30:00Z 

现在我想抓住从现在开始的下一个项目。

for (var j = 0; j < array.Times.length; j++) {
    if ((moment(array.Times[j])) > moment()) {
         // tried with new Date(), isAfter(), unix(), utc() ... "
         // got my item
    }
};

这可行,但我无法捕捉时区。
来自服务器的时间格式是UTC,对吗?
我需要的是语言环境时间...!?还是我完全错了?
在德国,我得到了 +2 小时的时区偏移。

http://jsfiddle.net/fool/fDpR4/

【问题讨论】:

    标签: jquery datetime momentjs


    【解决方案1】:

    您的日期字符串采用 ISO UTC 格式,因此根据您的时区创建的日期将具有不同的时间。因此,如果您创建moment("2014-04-14T13:30:00Z"),则在您的情况下,它的当地时间为 15:30。所以你的示例代码可能已经正常工作了,但我不知道你的要求。

    如果您希望将字符串解释为本地时间,最简单的解决方案是在末尾删除“Z”:

    for (var j = 0; j < myArray.length; j++) {
        var dateString = myArray[j].substr(0, myArray[j].length - 1);
        if (moment(dateString) > moment()) {
            console.log(dateString);
            console.log(moment(dateString).toDate());
            break;
        }
    }
    

    【讨论】:

    • hm,这是一个奇怪的问题...如果问题得到解决,我会报告。
    猜你喜欢
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    相关资源
    最近更新 更多