【问题标题】:Convert ISO DateTime to Millisecond将 ISO 日期时间转换为毫秒
【发布时间】:2013-12-05 13:20:35
【问题描述】:

我有一个 ISODateTime 格式:

2013-11-21T20:58:03+0000 

如何在 AngularJS 中将其转换为毫秒。

我使用了 DateTime.Parse() => 它在 chrome 上工作,但在 ios 上失败。 还有其他方法可以实现吗?

我的主要目的是找出当前时间和这次之间的差异(以分钟为单位):

var diff= (new Date(new Date().getTime() - Date.parse(item['myDate']))).getMinutes();

其中项目['myDate'] = 2013-11-21T20:58:03+0000

【问题讨论】:

    标签: javascript ios datetime isodate


    【解决方案1】:

    为了确保它适用于任何浏览器(如 iOS safari),我们只需拆分时间并创建新的日期实例。

    试试:

    $scope.item = {};   
    $scope.item.myDate = '2013-11-21T20:58:03+0000';
    
    var arr =  $scope.item.myDate.split(/[- :+T]/);
    var fixedDate = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4]);
    
    var currTime = new Date().getTime();
    var fixedTime = fixedDate.getTime();
    var tot = currTime -  fixedTime;
    
    var minutes = tot / 1000 / 60;
    
    console.log('tot', tot);
    console.log('minutes', minutes);
    

    演示Fiddle

    【讨论】:

    • 谢谢,但它给出了 NaN... 我记录了 fixedTime = new Date($scope.item.myDate).getTime();和 new Date(item['myDate']).getTime();两者都给出 NaN
    • 即使 new Date(item['myDate']) 给出 NaN
    • new Date(item['myDate']) 给出无效日期
    • 试试我改变的修复
    • 嗨,我终于能够通过从日期时间中修剪 +FFFF 值在 IOS 上解决此问题。这允许顺利执行整个代码。甚至你以前的小提琴。非常感谢..
    猜你喜欢
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多