Javascript:将字符串解析为Date作为LOCAL时区 [英] Javascript: parse a string to Date as LOCAL time zone

查看:189
本文介绍了Javascript:将字符串解析为Date作为LOCAL时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代表当前时间的字符串: 2015-11-24T19:40:00 。如何在Javascript中解析此字符串以获取此字符串表示的日期作为 LOCAL TIME 由于某些限制,我不能使用库时刻,但允许使用jquery。我知道以前有人问过这个问题,但是答案是使用的时刻

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

如果我在加利福尼亚州运行脚本,那么这个字符串将代表7PM太平洋时间,但是如果我在NY中运行脚本,那么这个字符串将代表东部时间?

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 2015年11月24日11:40:00 GMT-0800(太平洋标准时间)),

Chrome consumes it as UTC time (Tue Nov 24 2015 11:40:00 GMT-0800 (Pacific Standard Time)),

但Firefox会将其作为本地的太平洋时间( Tue Nov 24 2015 19:40:00 GMT-0800(太平洋标准时间)

but Firefox consumes it as my local PACIFIC time (Tue Nov 24 2015 19:40:00 GMT-0800 (Pacific Standard Time))

我尝试向 str 添加Z,像这样 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.

如果将日期调用为函数并传递不带时区的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.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的字符串只接受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:将字符串解析为Date作为LOCAL时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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