XML 日期时间到 Javascript 日期对象

发布于 2024-12-16 13:25:56 字数 340 浏览 0 评论 0原文

所以我正在编写一个使用 ajax 从基于 xml 的 api 获取的应用程序。 api 以以下格式返回日期:

2011-11-12T13:00:00-07:00

我需要将其作为标准 JavaScript 日期对象获取,

var myDate = new Date('2011-11-12T13:00:00-07:00');

该对象在除 ie8 和 ie7 之外的每个浏览器中都可以正常工作。我只是不明白为什么,而且似乎找不到任何关于如何专门针对 ie7-8 进行格式化的文档。我知道必须有一种聪明的方法来做到这一点。请帮忙。谢谢。

So I am writing an application using ajax getting from a xml based api. The api returns dates in the following format:

2011-11-12T13:00:00-07:00

I need to get this as a standard JavaScript date object

var myDate = new Date('2011-11-12T13:00:00-07:00');

which works great in every browser BUT ie8 and ie7. I just don't understand why and can't seem to find any documentation on how to format this specifically for ie7-8. I know there has to be a smart way to do this. Please help. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一腔孤↑勇 2024-12-23 13:25:56

唯一聪明的方法是解析字符串并手动创建日期对象。这并不难:

var dateString = '2011-11-12T13:00:00-07:00';

function dateFromString(s) {
  var bits = s.split(/[-T:]/g);
  var d = new Date(bits[0], bits[1]-1, bits[2]);
  d.setHours(bits[3], bits[4], bits[5]);

  return d;
}

您可能想要设置该位置的时间,因此您需要将时区偏移量应用于创建的时间对象,这并不难,只是 javascript 日期对象将以分钟为单位的偏移量添加到时间中以获取 UTC,而大多数时间戳减去偏移量(即 -7:00 表示 UTC - 7 小时才能获取本地时间,但 javascript 日期时区偏移量将为 +420)。

允许偏移:

function dateFromString(s) {
  var bits = s.split(/[-T:+]/g);
  var d = new Date(bits[0], bits[1]-1, bits[2]);
  d.setHours(bits[3], bits[4], bits[5]);

  // Get supplied time zone offset in minutes
  var offsetMinutes = bits[6] * 60 + Number(bits[7]);
  var sign = /\d\d-\d\d:\d\d$/.test(s)? '-' : '+';

  // Apply the sign
  offsetMinutes = 0 + (sign == '-'? -1 * offsetMinutes : offsetMinutes);

  // Apply offset and local timezone
  d.setMinutes(d.getMinutes() - offsetMinutes - d.getTimezoneOffset())

  // d is now a local time equivalent to the supplied time
  return d;
} 

当然,如果您使用 UTC 日期和时间,那就简单得多,然后您只需创建一个本地日期对象,setUTCHours,然后创建日期,就可以了 - 日期对象将执行时区操作(前提是当然,本地系统已正确设置...)。

The only smart way is to parse the string and manually create a date object. It's not hard:

var dateString = '2011-11-12T13:00:00-07:00';

function dateFromString(s) {
  var bits = s.split(/[-T:]/g);
  var d = new Date(bits[0], bits[1]-1, bits[2]);
  d.setHours(bits[3], bits[4], bits[5]);

  return d;
}

You probably want to set the time for the location, so you need to apply the timezone offset to the created time object, it's not hard except that javascript date objects add the offset in minutes to the time to get UTC, whereas most timestamps subtract the offset (i.e. -7:00 means UTC - 7hrs to get local time, but the javascript date timezone offset will be +420).

Allow for offset:

function dateFromString(s) {
  var bits = s.split(/[-T:+]/g);
  var d = new Date(bits[0], bits[1]-1, bits[2]);
  d.setHours(bits[3], bits[4], bits[5]);

  // Get supplied time zone offset in minutes
  var offsetMinutes = bits[6] * 60 + Number(bits[7]);
  var sign = /\d\d-\d\d:\d\d$/.test(s)? '-' : '+';

  // Apply the sign
  offsetMinutes = 0 + (sign == '-'? -1 * offsetMinutes : offsetMinutes);

  // Apply offset and local timezone
  d.setMinutes(d.getMinutes() - offsetMinutes - d.getTimezoneOffset())

  // d is now a local time equivalent to the supplied time
  return d;
} 

Of course is it much simpler if you use UTC dates and times, then you just create a local date object, setUTCHours, then date and you're good to go - the date object will do the timezone thing (provided the local system has it set correctly of course...).

简单 2024-12-23 13:25:56

似乎这应该适用于 Date()格式

这又如何呢?

var millisecsSince1970 = Date.parse('2011-11-12T13:00:00-07:00');
var date = new Date(millisecsSince1970);

Seems like that should work per MSDN docs on Date() and formatting.

What about this?

var millisecsSince1970 = Date.parse('2011-11-12T13:00:00-07:00');
var date = new Date(millisecsSince1970);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文