创建包含时区标识符的新日期对象 [英] Creating new date object which includes timezone identifier

查看:110
本文介绍了创建包含时区标识符的新日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Facebook的API查询事件开始时间。最常见的是,他们返回字符串如下:

  2014-04-02T20:00:00-0600 

但有时候,它的返回如下:

  2014-04-19 

当我尝试创建一个新的日期对象,它不适用于时区标识符。



但是,这两个工作:

  var d = new Date('2014-04-02T20:00:00')

  var d = new Date('2014-04-19')

但是我需要这个工作:

  var d = new Date('2014-04-02T20:00:00-0600')
//返回无效日期

我正在尝试避免解析每个start_time字符串,以查看是否需要删除时区标识符,因为我正在处理数百个事件。


$ b $有没有更简单的方法来做我想要的,或者我实际需要删除

解决方案

您可以使用以下方法解析具有偏移量的ISO 8601字符串。如果字符串没有偏移量,则假定为Z(即UTC / ECMA-262 ):

  / *解析带有偏移量的
**的ISO字符串'2014-04-02T20:00:00-0600'
**'2014-04-02T20:00:00Z'
**
**提供十进制秒(如果提供)
**例如'2014-04-02T20:00:00.123-0600'
**
**如果没有提供偏移量(或者是Z),则视为UTC(根据ECMA-262)
* *如果只有日期,例如'2014-04-02',视为UTC日期(按ECMA-262)
* /
函数parseISOString(s){
var t = s.split(/ \D + G);
var hasOffset = /\d{2}[-+]\d{4}$/.test(s);

//是否存在十进制秒是否更改偏移字段和ms值
var hasDecimalSeconds = / T\d {2}:\d {2}:\d {2 } \.\d + / i.test(一个或多个);
var offset = hasDecimalSeconds? t [7]:t [6];
var ms = hasDecimalSeconds? t [6]:0;
var offMin,offSign,min;

//如果有偏移量,则将其应用到几分钟以获取UTC时间值
if(hasOffset){
offMin = 60 * offset / 100 + offset%100;
offSign = /-\d{4}$/.test(s)? -1:1;
}
min = hasOffset? + t [4] - offMin * offSign:(t [4] || 0);

//根据UTC值返回一个日期对象
return new Date(Date.UTC(t [0], - t [1],t [2],t [3 ] || 0,min,t [5] || 0,ms));
}

//测试
console.log(parseISOString('2014-04-02T20:00:00-0600')toISOString()); // 2014-04-03T02:00:00.000Z
console.log(parseISOString('2014-04-02T20:00:00Z')。toISOString()); // 2014-04-02T20:00:00.000Z
console.log(parseISOString('2014-04-02T20:00:00')。toISOString()); // 2014-04-02T20:00:00.000Z
console.log(parseISOString('2014-04-02T20:00:00.123-0600')。toISOString()); // 2014-04-03T02: 00:00.123Z
console.log(parseISOString('2014-04-02T20:00:00.123')。toISOString()); // 2014-04-02T20:00:00.123Z
console.log(parseISOString('2014-04-02')。toISOString()); // 2014-04-02T00:00:00.000Z

请注意,如果这将创建一个日期对象根据系统设置对主机环境的时区进行偏移。日期的时间值将是提供的时间戳(在本例中为2014-04-03T02:00:00Z)的UTC时间。您可以确认使用 toISOString 方法(需要IE9或更高版本或其他现代浏览器)。



编辑



请注意,您不能设置Date对象的时区偏移量。它们具有表示UTC时刻的时间值,以及基于系统设置的时区偏移量。您可以使用诸如 getFullYear getHours 之类的标准Date方法读取本地日期和时间值,如果使用UTC方法,则可以使用UTC值(例如, getUTCFullYear getUTCHours )。



编辑2



只允许使用日期格式(将其视为UTC)


I am querying event start times from Facebook's API. Most often, they return the string like the following:

2014-04-02T20:00:00-0600

But sometimes, it is returned like:

2014-04-19

When I try to create a new date object, it does not work with the timezone identifier.

However, both of these work:

var d = new Date('2014-04-02T20:00:00')

or

var d = new Date('2014-04-19')

But I need this to work:

var d = new Date('2014-04-02T20:00:00-0600')
// returns Invalid Date

I'm trying to avoid parsing each start_time string to see if I need to remove the timezone identifier since I am working with hundreds of events.

Is there an easier way to do what I'm trying, or do I actually need to remove the timezone identifier for each date that has it?

解决方案

You can parse an ISO 8601 string with offset using the following. If the string has no offset, it is assumed to be Z (i.e. UTC per ECMA-262):

/* Parse an ISO string with an offset
**   e.g. '2014-04-02T20:00:00-0600'
**        '2014-04-02T20:00:00Z'
**
** Allows decimal seconds if supplied
**   e.g. '2014-04-02T20:00:00.123-0600'
**
** If no offset is supplied (or it's Z), treat as UTC (per ECMA-262)
** If date only, e.g. '2014-04-02', treat as UTC date (per ECMA-262)
*/
function parseISOString(s) {
  var t = s.split(/\D+/g);
  var hasOffset = /\d{2}[-+]\d{4}$/.test(s);

  // Whether decimal seconds are present changes the offset field and ms value
  var hasDecimalSeconds = /T\d{2}:\d{2}:\d{2}\.\d+/i.test(s);
  var offset = hasDecimalSeconds? t[7] : t[6];
  var ms = hasDecimalSeconds? t[6] : 0;
  var offMin, offSign, min;

  // If there's an offset, apply it to minutes to get a UTC time value
  if (hasOffset) {
    offMin = 60 * offset / 100 + offset % 100;
    offSign = /-\d{4}$/.test(s)? -1 : 1;
  }
  min = hasOffset? +t[4] - offMin * offSign : (t[4] || 0);

  // Return a date object based on UTC values
  return new Date(Date.UTC(t[0], --t[1], t[2], t[3]||0, min, t[5]||0, ms));
}

// Tests
console.log(parseISOString('2014-04-02T20:00:00-0600').toISOString());    // 2014-04-03T02:00:00.000Z
console.log(parseISOString('2014-04-02T20:00:00Z').toISOString());        // 2014-04-02T20:00:00.000Z
console.log(parseISOString('2014-04-02T20:00:00').toISOString());         // 2014-04-02T20:00:00.000Z
console.log(parseISOString('2014-04-02T20:00:00.123-0600').toISOString());// 2014-04-03T02:00:00.123Z
console.log(parseISOString('2014-04-02T20:00:00.123').toISOString());     // 2014-04-02T20:00:00.123Z
console.log(parseISOString('2014-04-02').toISOString());                  // 2014-04-02T00:00:00.000Z

Note that if this will create a date object with an offset for the timezone of the host environment based on system settings. The timevalue of the Date will be the equivalent UTC time for the supplied timestamp (in this case, '2014-04-03T02:00:00Z'). You can confirm that using the toISOString method (requires IE9 or higher, or other modern browser).

Edit

Note that you can't set the timezone offset of a Date object. They have a time value that represents a UTC moment in time, and a timezone offset that is based on system settings. You can read "local" date and time values using standard Date methods like getFullYear and getHours, or UTC values if UTC methods are used (e.g. getUTCFullYear, getUTCHours).

Edit 2

Allowed for date only format (treats it as UTC).

这篇关于创建包含时区标识符的新日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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