Javascript日期在iOS上是无效的

时间:2022-09-01 15:38:25

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?

我正在开发一款基于手机的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?

区别从何而来?

4 个解决方案

#1


69  

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还是local time。您可以使用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 /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不像Android一样支持Javascript日期函数,或者支持不同的格式?

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


-1  

Try with var d = new Date("2015/12/31 00:00:00"); Works for me.

尝试var d = new Date(“2015/12/31 00:00”);为我工作。

#4


-2  

Also

var d = new Date("2015/12/31T00:00:00");

works for me :)

为我工作)

Thanks @dda

由于@dda

#1


69  

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还是local time。您可以使用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 /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不像Android一样支持Javascript日期函数,或者支持不同的格式?

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


-1  

Try with var d = new Date("2015/12/31 00:00:00"); Works for me.

尝试var d = new Date(“2015/12/31 00:00”);为我工作。

#4


-2  

Also

var d = new Date("2015/12/31T00:00:00");

works for me :)

为我工作)

Thanks @dda

由于@dda