javascript iOS 设备上的日期返回 NaN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26657353/
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
Date on iOS device returns NaN
提问by Sjoerd de Wit
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:
我目前正在使用 ionic 和 angularjs 开发一个基于cordova 的web 应用程序。现在我已经创建了一个服务,它以我的客户想要的方式返回一个格式化的时间。问题在于,虽然它可以在 android 和浏览器上运行,但它在 iOS 设备上显示 NaN。我插入的日期来自时间戳:NOW() 格式的数据库,是否有解决方法?这是我的约会服务:
.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;
}
}]);
回答by Sjoerd de Wit
Fixed this thanks to @Ian his comment ,
感谢@Ian 他的评论修复了这个问题,
changed this:
改变了这一点:
var actiondate = new Date(date);
to this:
对此:
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);
回答by Key Shang
The reason of the problem is iPhone Safari doesn't support the Y-m-d H:i:s
(ISO 8601) date format. I have encountered this problem in 2017/7/19, I do not understand why Safari did not fix the problem after two years.
问题的原因是 iPhone Safari 不支持Y-m-d H:i:s
(ISO 8601) 日期格式。我在 2017/7/19 遇到过这个问题,不明白为什么 Safari 两年后没有修复这个问题。
I refer to the answer of Sjoerd, thanks Sjoerd, just rewrite it to a function to do the date conversion when you have many date need to be processed. After you get a date(must be Y-m-d H:i:s
format) from server, you could use the function convert the date to the format which iOS device could resolve and other browsers could resolve too.
我参考了Sjoerd的回答,感谢Sjoerd,当你有很多日期需要处理时,只需将其重写为一个函数来进行日期转换。从服务器获取日期(必须为Y-m-d H:i:s
格式)后,您可以使用该功能将日期转换为 iOS 设备可以解析的格式,其他浏览器也可以解析。
function convertDateForIos(date) {
var arr = date.split(/[- :]/);
date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]);
return date;
}
I encountered the problem when I write ajax for date, hope the solution will help others encountered the annoying problem.
我在写ajax for date的时候遇到了这个问题,希望解决的方法能帮到其他人遇到这个烦人的问题。
回答by Zeeshan Ahmed
install momentjs to by
npm install moment --save
通过npm install moment --save 安装 momentjs
intitilize the new date with
var date = moment(yourDate).format()
var formatedDate = new data(date)
使用
var date = moment(yourDate).format()
var formatedDate = new data(date)初始化新日期
hope this resolve the issue
希望这能解决问题