I'm working on a Phonegap-based iOS app, which is already done for Android. The following lines are working fine for Android but not for iOS. Why?
我正在开发基于Phonegap的iOS应用程序,该应用程序已经针对Android完成。以下几行适用于Android,但不适用于iOS。为什么?
var d = new Date("2015-12-31 00:00:00");console.log(d.getDate() + '. ' + d.getMonth() + ' ' + d.getFullYear();
Result for Android:
Android的结果:
31.11 2015
Result on iOS:
iOS上的结果:
NaN. NaN NaN
Where is the difference coming from?
差异来自哪里?
5 个解决方案
#1
76
Your date string is not in a format specified to work with new Date
. The only format in the spec is a simplified version of ISO-8601, but that was only added in ES5 and so support may be touch and go. Your string isn't in that format, but it's really close.
您的日期字符串的格式不符合使用新日期的格式。规范中唯一的格式是ISO-8601的简化版本,但仅在ES5中添加,因此支持可能是触摸和去。你的字符串不是那种格式,但它真的很接近。
If you change the space to a T
, you'll be in spec:
如果您将空间更改为T,您将符合规范:
var dateString = "2015-12-31 00:00:00";var d = new Date(dateString.replace(' ', 'T'));
(I'm assuming you're not actually using a string literal, hence the replace
call.)
(我假设你实际上并没有使用字符串文字,因此替换调用。)
Note that there was an error in the ES5 specification which was corrected in ES2015 (ES6): What happens when there's no timezone indicator on the string. In ISO-8601, no indicator means "local time," but the ES5 specification said that it defaults to Z
(GMT). They fixed it in the ES2015 specification, but unfortunately some JavaScript engines followed the ES5 specification and others followed ISO-8601 (and now ES2015), so for solid cross-browser support, you need to include a timezone indicator because otherwise you don't know whether it will be interpreted as GMT or local time. You're allowed to use Z
for GMT or +/-
followed by HH:MM
to gie an offset. (Abbreviations like CST
are not allowed, as there's no standard for them.)
请注意,ES5规范中存在错误,该错误已在ES2015(ES6)中得到纠正:当字符串上没有时区指示符时会发生什么。在ISO-8601中,没有指标表示“本地时间”,但ES5规范表示它默认为Z(GMT)。他们在ES2015规范中修复了它,但遗憾的是一些JavaScript引擎遵循ES5规范,其他一些遵循ISO-8601(现在是ES2015),因此为了实现跨浏览器支持,您需要包含时区指示器,否则您不会知道它是否会被解释为GMT或当地时间。您可以使用Z表示GMT或+/-,然后使用HH:MM来计算偏移量。 (不允许使用像CST这样的缩写词,因为它们没有标准。)
If they don't support that yet, even though it's undocumented, there's near universal support for YYYY/MM/DD HH:MM:SS
. So:
如果他们还没有支持,即使它没有证件,也几乎普遍支持YYYY / MM / DD HH:MM:SS。所以:
var dateString = "2015-12-31 00:00:00";var d = new Date(dateString.replace(/-/g, '/'));
#2
5
I can't tell you why. Maybe because iOS doesn't support the Javascript Date function as well as Android, or support a different format?
我不能告诉你为什么。也许是因为iOS不支持Javascript Date功能以及Android,或支持不同的格式?
But I can give you a workaround:
但我可以给你一个解决方法:
var s = "2015-12-31 00:00:00".split(" ")[0].split("-"), d = new Date( s[0], s[1], s[2], 0, 0, 0 );console.log(d);
var s = "2015-12-31 00:00:00".replace(/[ :]/g, "-").split("-"), d = new Date( s[0], s[1], s[2], s[3], s[4], s[5] );console.log(d);
#3
0
A solution that works for both IOS and Android, and avoid string manipulation when it isn't required is
适用于IOS和Android的解决方案,并且在不需要时避免字符串操作
let fullDate = "1991-03-29 00:00:00";let date = new Date(fullDate);// In case its IOS, parse the fulldate parts and re-create the date object.if(Number.isNaN(date.getMonth())) { let arr = fullDate.split(/[- :]/); date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]);}
#4
-2
Try with var d = new Date("2015/12/31 00:00:00");
Works for me.
尝试使用var d = new Date(“2015/12/31 00:00:00”);适合我。
#5
-3
Also
var d = new Date("2015/12/31T00:00:00");
works for me :)
适合我:)
Thanks @dda
#1
76
Your date string is not in a format specified to work with new Date
. The only format in the spec is a simplified version of ISO-8601, but that was only added in ES5 and so support may be touch and go. Your string isn't in that format, but it's really close.
您的日期字符串的格式不符合使用新日期的格式。规范中唯一的格式是ISO-8601的简化版本,但仅在ES5中添加,因此支持可能是触摸和去。你的字符串不是那种格式,但它真的很接近。
If you change the space to a T
, you'll be in spec:
如果您将空间更改为T,您将符合规范:
var dateString = "2015-12-31 00:00:00";var d = new Date(dateString.replace(' ', 'T'));
(I'm assuming you're not actually using a string literal, hence the replace
call.)
(我假设你实际上并没有使用字符串文字,因此替换调用。)
Note that there was an error in the ES5 specification which was corrected in ES2015 (ES6): What happens when there's no timezone indicator on the string. In ISO-8601, no indicator means "local time," but the ES5 specification said that it defaults to Z
(GMT). They fixed it in the ES2015 specification, but unfortunately some JavaScript engines followed the ES5 specification and others followed ISO-8601 (and now ES2015), so for solid cross-browser support, you need to include a timezone indicator because otherwise you don't know whether it will be interpreted as GMT or local time. You're allowed to use Z
for GMT or +/-
followed by HH:MM
to gie an offset. (Abbreviations like CST
are not allowed, as there's no standard for them.)
请注意,ES5规范中存在错误,该错误已在ES2015(ES6)中得到纠正:当字符串上没有时区指示符时会发生什么。在ISO-8601中,没有指标表示“本地时间”,但ES5规范表示它默认为Z(GMT)。他们在ES2015规范中修复了它,但遗憾的是一些JavaScript引擎遵循ES5规范,其他一些遵循ISO-8601(现在是ES2015),因此为了实现跨浏览器支持,您需要包含时区指示器,否则您不会知道它是否会被解释为GMT或当地时间。您可以使用Z表示GMT或+/-,然后使用HH:MM来计算偏移量。 (不允许使用像CST这样的缩写词,因为它们没有标准。)
If they don't support that yet, even though it's undocumented, there's near universal support for YYYY/MM/DD HH:MM:SS
. So:
如果他们还没有支持,即使它没有证件,也几乎普遍支持YYYY / MM / DD HH:MM:SS。所以:
var dateString = "2015-12-31 00:00:00";var d = new Date(dateString.replace(/-/g, '/'));
#2
5
I can't tell you why. Maybe because iOS doesn't support the Javascript Date function as well as Android, or support a different format?
我不能告诉你为什么。也许是因为iOS不支持Javascript Date功能以及Android,或支持不同的格式?
But I can give you a workaround:
但我可以给你一个解决方法:
var s = "2015-12-31 00:00:00".split(" ")[0].split("-"), d = new Date( s[0], s[1], s[2], 0, 0, 0 );console.log(d);
var s = "2015-12-31 00:00:00".replace(/[ :]/g, "-").split("-"), d = new Date( s[0], s[1], s[2], s[3], s[4], s[5] );console.log(d);
#3
0
A solution that works for both IOS and Android, and avoid string manipulation when it isn't required is
适用于IOS和Android的解决方案,并且在不需要时避免字符串操作
let fullDate = "1991-03-29 00:00:00";let date = new Date(fullDate);// In case its IOS, parse the fulldate parts and re-create the date object.if(Number.isNaN(date.getMonth())) { let arr = fullDate.split(/[- :]/); date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]);}
#4
-2
Try with var d = new Date("2015/12/31 00:00:00");
Works for me.
尝试使用var d = new Date(“2015/12/31 00:00:00”);适合我。
#5
-3
Also
var d = new Date("2015/12/31T00:00:00");
works for me :)
适合我:)
Thanks @dda