iOS设备上的日期返回NaN [英] Date on iOS device returns NaN

查看:216
本文介绍了iOS设备上的日期返回NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用ionic和angularjs开发基于Cordova Web的应用程序.现在,我创建了一个服务,该服务以我的客户希望的方式返回格式化时间..问题是,尽管它可以在android和浏览器上运行,但会在iOS设备上显示NaN.我插入的日期来自时间戳为NOW()的数据库,是否有针对此的解决方法? 这是我的约会服务:

i'm currently developing a cordova web based application with ionic and angularjs. now i've created a service that returns a formatted time the way my client wants it.. the problem with this is that whilst it works on android and in browser, it displays NaN on an iOS device. The date i insert is from a database in timestamp : NOW() format, is there a fix for this? this is my date service:

.factory('displaydate',['$filter', function($filter) {
  return function (date){
    var maandarray = new Array('Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'); 
    var actiondate = new Date(date);
    var today = new Date();
    if(today.getDate() == actiondate.getDate()){
        var hourssince =   today.getHours() - actiondate.getHours()
        var minutessince =   today.getMinutes() - actiondate.getMinutes()
        var secondssince =   today.getSeconds() - actiondate.getSeconds()
        if(hourssince > 0){
            date = hourssince+'u';
        }else if(minutessince > 0){
            date = minutessince+'m';
        }else{
            date = secondssince+'s';
        }
    }else{
        var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
        var diffDays = Math.round(Math.abs((today.getTime() - actiondate.getTime())/(oneDay)));
        if(diffDays > 28){
            var identifier = actiondate.getMonth() - 1;
            var month = maandarray[identifier];
            date = $filter('date')(actiondate,"d ") + month +  $filter('date')(actiondate," yy " + " HH:" + "mm");
        }else{ 
            date = diffDays+'d';
        }
    }
    return date;
  }
}]);

推荐答案

感谢@Ian的评论,解决了此问题

Fixed this thanks to @Ian his comment ,

更改了此内容:

var actiondate = new Date(date);

对此:

var t = date.split(/[- :]/);

// Apply each element to the Date function
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
var actiondate = new Date(d);

这篇关于iOS设备上的日期返回NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆