Javascript:将字符串解析为日期作为本地时区

Javascript: parse a string to Date as LOCAL time zone(Javascript:将字符串解析为日期作为本地时区)
本文介绍了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:

  1. ES5 之前的实现可以将其视为任何东西,甚至是 NaN(例如 IE 8)
  2. 符合 ES5 的实现会将其视为 UTC 时区
  3. 符合 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:将字符串解析为日期作为本地时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

具体实现代码如下: laydate.render({ elem: '#start_time', min:0, //,type: 'date' //默认,可不填}); 只要加一个min参数,就可以控制了。0表示之前的日期不可
在Layui中,如果你想设置日期时间选择器(datetime)的默认结束时间为当天的23:59:59,你可以使用如下代码: laydate.render({ elem: '#test10' ,type: 'datetime' ,range: true ,max: '{:date("Y-m-d 23:59:59")}' ,ready: function(date){ $(".layui-laydat
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)