Javascript: parse a string to Date as LOCAL time zone(Javascript:将字符串解析为日期作为本地时区)
问题描述
我有一个代表当前时间的字符串:2015-11-24T19:40:00
.如何在 Javascript 中解析此字符串以获取由此字符串表示为 LOCAL TIME 的日期?由于某些限制,我不能使用库 moment
,但允许使用 jquery.我知道之前有人问过这个问题,但是答案使用了moment
I have a string representing the current time: 2015-11-24T19:40:00
. How do I parse this string in Javascript to get a Date represented by this string as the LOCAL TIME? Due to some restriction, I cannot use the library moment
, but jquery is allowed. I know that someone has asked this question before, but the answer used moment
例如,如果我在加利福尼亚运行脚本,那么这个字符串代表太平洋时间晚上 7 点,但如果我在纽约运行脚本,那么这个字符串代表东部时间?
For example, if I run the script in California, then this string would represent 7PM pacific time, but if I run the script in NY then this string would represent Eastern Time?
我尝试了以下方法,但 Chrome 和 Firefox 给出了不同的结果:
I tried the following but Chrome and Firefox give me different results:
var str = "2015-11-24T19:40:00";
var date = new Date(str);
Chrome 将其作为 UTC 时间(Tue Nov 24 2015 11:40:00 GMT-0800(太平洋标准时间)
),
Chrome consumes it as UTC time (Tue Nov 24 2015 11:40:00 GMT-0800 (Pacific Standard Time)
),
但 Firefox 将其作为我的本地 PACIFIC 时间使用(Tue Nov 24 2015 19:40:00 GMT-0800(Pacific Standard Time)
)
but Firefox consumes it as my local PACIFIC time (Tue Nov 24 2015 19:40:00 GMT-0800 (Pacific Standard Time)
)
我尝试将Z"添加到 str
,就像这样 var date = new Date(str+"Z");
,然后两个浏览器都给我 UTC 时间.是否有任何类似于 "Z"
的字母告诉所有浏览器(至少 chrome、Firefox 和 Safari)将字符串解析为本地时区?
I tried adding "Z" to str
, like this var date = new Date(str+"Z");
, then both browsers give me UTC time. Is there any similar letter to "Z"
which tells all browsers (at least chrome, Firefox and Safari) to parse the string as local time zone?
推荐答案
强烈建议不要使用 Date 构造函数或 Date.parse(本质上是相同的东西)解析日期字符串.
Parsing of date strings using the Date constructor or Date.parse (which are essentially the same thing) is strongly recommended against.
如果 Date 作为函数被调用并传递了一个没有时区的 ISO 8601 格式的日期字符串(例如 2015-11-24T19:40:00),您可能会得到以下结果之一:
If Date is called as a function and passed an ISO 8601 format date string without a timezone (such as 2015-11-24T19:40:00), you may get one of the following results:
- ES5 之前的实现可以将其视为任何东西,甚至是 NaN(例如 IE 8)
- 符合 ES5 的实现会将其视为 UTC 时区
- 符合 ECMAScript 2015 的实现会将其视为本地(与 ISO 8601 一致)
Date 对象有一个 UTC 时间值和一个基于系统设置的偏移量.当您将 Date 发送到输出时,您看到的通常是 Date.prototype.toString 的结果,它是一个依赖于实现的、人类可读的字符串,表示日期和时间,通常位于基于系统设置.
A Date object has a time value which is UTC, and an offset based on system settings. When you send a Date to output, what you see is usually the result of Date.prototype.toString, which is an implementation dependent, human readable string representing the date and time, usually in a timezone based on system settings.
解析日期字符串的最佳方法是手动进行.如果您确信格式是一致且有效的,那么将 ISO 格式字符串解析为本地日期非常简单:
The best way to parse date strings is to do it manually. If you are assured that the format is consistent and valid, then parsing an ISO format string as a local date is as simple as:
/* @param {string} s - an ISO 8001 format date and time string
** with all components, e.g. 2015-11-24T19:40:00
** @returns {Date} - Date instance from parsing the string. May be NaN.
*/
function parseISOLocal(s) {
var b = s.split(/D/);
return new Date(b[0], b[1]-1, b[2], b[3], b[4], b[5]);
}
document.write(parseISOLocal('2015-11-24T19:40:00'));
请注意,使用 Date.parse 解析 ISO 字符串仅接受 UTC,它不接受任何其他时区指定(如果缺少上述行为,请注意上述行为).
Note that parsing of ISO strings using Date.parse only accepts UTC, it does not accept any other timezone designation (noting the above behaviour if it's missing).
这篇关于Javascript:将字符串解析为日期作为本地时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Javascript:将字符串解析为日期作为本地时区
基础教程推荐
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01