保证在现代浏览器中将JavaScript日期格式解释为本地时间 [英] JavaScript date-only format guaranteed to be interpreted as local time in modern browsers

查看:246
本文介绍了保证在现代浏览器中将JavaScript日期格式解释为本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种日期格式(只有年,月,日,没有时间组成部分),在所有合理的现代浏览器中,将保证解释为当地时间,

I am looking for a date format (only year, month and day, no time component), that will be guaranteed to be interpreted as local time, in all reasonably modern browsers, when passed to the Date() constructor in JavaScript.

(3个字母的英语月份缩写)(2位数日期)(4位数年份) 格式似乎工作。当我在我的英文系统上在Chrome中使用 new Date('Apr 01 2015')时,我的机器在东部时区,我回到 Wed Apr 01 2015 00:00:00 GMT-0400(Eastern Daylight Time)这正是我想要的。

The (3-letter English month abbreviation) (2-digit date) (4-digit year) format seems to work. When I do new Date('Apr 01 2015') on my English system, in Chrome, with my machine being in the Eastern timezone, I get back Wed Apr 01 2015 00:00:00 GMT-0400 (Eastern Daylight Time) which is exactly what I want.

不确定这个(3个字母的英语月份缩写)(2位数的日期)(4位数年份)格式是否保证以相同的方式解释,浏览器和浏览器/操作系统区域设置/语言。

However, I am not sure if this (3-letter English month abbreviation) (2-digit date) (4-digit year) format is guaranteed to be interpreted in the same way regardless of browser and browser/OS locale/language.

这种格式在不同的(相当现代的)浏览器和语言环境中的工作方式是否相同?如果没有,那么还有其他格式可以工作吗?

Will this format work the same way across different (reasonably modern) browsers and locales? If not, then is there some other format that will work?

请注意,ISO-8601没有回答这个问题。 ISO-8601仅日期字符串解释为以UTC为时间,而不是本地时间,因此 new Date('2015-04-01')解释为 Tue March 31 2015 20:00:00 GMT-0400(Eastern Daylight Time),即4月1日变成3月31日,这不是我要找的。 >

Please note that ISO-8601 doesn't answer this question. ISO-8601 date-only strings are interpreted as being in UTC, and not in local time, so new Date('2015-04-01') is interpreted as Tue Mar 31 2015 20:00:00 GMT-0400 (Eastern Daylight Time), i.e. April 1 turns into March 31, which is not what I'm looking for.

推荐答案


我正在寻找一个日期格式(只有年,月,组件),在传递到JavaScript中的Date()构造函数时,将确保在所有合理的现代浏览器中被解释为本地时间。

I am looking for a date format (only year, month and day, no time component), that will be guaranteed to be interpreted as local time, in all reasonably modern browsers, when passed to the Date() constructor in JavaScript.

没有一个。在实践中,有许多格式可以被所有使用的浏览器可靠地解析,但是ECMA-262不需要它们来支持ECMAScript的实现。

There isn't one. There are a number of formats that, in practice, are reliably parsed by all browsers in use however none of them are required by ECMA-262 to be supported by ECMAScript implementations.

有一种格式是ISO 8601的一个版本,在ES5和ECMAScript 2015中指定,但是没有时区的日期在ES5中被视为UTC,在ECMAScript 2015中被视为local。

There is one format, a version of ISO 8601, that is specified in ES5 and ECMAScript 2015 however dates without a time zone are to be treated as UTC in ES5 and "local" in ECMAScript 2015.

被实现者看作打破网络,所以虽然一些浏览器实现了一段时间,他们现在恢复为ES5行为,并将缺少的时区视为UTC。

But that was seen by implementers as "breaking the web", so while some browsers implemented that for a while, they have now reverted to ES5 behaviour and treat a missing timezone as UTC.

格式的小改变,例如将分隔符从 - 更改为/会导致恢复为实现相关的行为,并可能导致日期被处理作为UTC或本地(虽然大多数似乎将2015/12/17视为本地)。

A small change in format such as changing the separator from "-" to "/" causes a reversion to implementation dependent behaviour and may cause the date to be treated as UTC or local (though most seem to treat 2015/12/17 as local).

因此,如果你想要一个保证一个。然而,如果你准备写一个2行函数,你可以根据你喜欢的时区可靠地解析ISO 8601类似的字符串。如果您希望将其视为本地(即设置为00:00:00并根据系统设置进行偏移),则:

So if you want a guarantee, there isn't one. However, if you are prepared to write a 2 line function, you can parse an ISO 8601 like string reliably based on whatever time zone you like. If you want it to be treated as local (i.e. set to 00:00:00 and offset based on system settings), then:

function parseISOLike(s) {
    var b = s.split(/\D/);
    return new Date(b[0], b[1]-1, b[2])
}

document.write(parseISOLike('2015-12-17'));

将执行此作业(不检查有效值),并允许分隔符为任何非数字,因此2015 / 12/17,2015-12-17和2015.12.17将正确解析。

will do the job (without checking for valid values) and allow the separator to be any non–digit, so 2015/12/17, 2015-12-17 and 2015.12.17 will all be correctly parsed.

以上部分已根据 Matt Johnson 的输入编辑。

Parts of the above have been edited thanks to input from Matt Johnson.

这篇关于保证在现代浏览器中将JavaScript日期格式解释为本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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