如何在javascript中将datetime微格式转换为本地时间? [英] How can I convert datetime microformat to local time in javascript?

查看:88
本文介绍了如何在javascript中将datetime微格式转换为本地时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页目前正在使用 datetime microformat 来显示时间戳,但我只显示了我自己时区的人类可读时间:

I have a page that is currently using the datetime microformat to display a timestamp, but I have only been showing the human-readable time for my own time zone:

<abbr class="published" title="2009-01-09T09:16:00-05:00">
Friday, January 9, 2009 at 9:16 am (EST)</abbr>

我想要做的是将abbr标签的innerHTML重写为相同的格式,但是在用户的本地时区。因此,对于西雅图的读者,上述内容应转换为:

What I'd like to do is rewrite the innerHTML for the abbr tag to be the same format, but in the user's local timezone. So for a reader in Seattle, the above should be converted to:

<abbr class="published" title="2009-01-09T09:16:00-05:00">
Friday, January 9, 2009 at 6:16 am (PST)</abbr>

我看过 Javascript Date对象,它允许我获取本地时区偏移量。但我有一些问题:

I've looked at the Javascript Date object, which allows me to get the local timezone offset. But I have a few problems:


  1. 我没有看到从 ISO-8601 时间戳。 (我想如果没有更快的方法,我可以用子串或正则表达式进行解析。)

  1. I don't see an easy way to create a new Date object from an ISO-8601 timestamp. (I suppose I could parse with substrings or regex if there's no faster way.)

我没有办法获得时区的命名缩写。例如,对于西雅图的读者,我希望有时间将(PST)附加到最后,否则该用户不清楚时间戳已被转换(特别是如果他是常客和已经习惯了我的时间在EST)。

I don't see a way to get the named abbreviation for the timezone. For example, for a reader in Seattle, I'd want the time to have "(PST)" appended to the end, otherwise it is not clear to that user that the timestamp has been converted (especially if he is a frequent visitor and has become accustomed to the fact that my times are in EST).


推荐答案

以下是解析ISO时间戳的我的代码:

Here is code of mine that parses an ISO timestamp:

function isoDateStringToDate (datestr) {
  if (! this.re) {
    // The date in YYYY-MM-DD or YYYYMMDD format
    var datere = "(\\d{4})-?(\\d{2})-?(\\d{2})";
    // The time in HH:MM:SS[.uuuu] or HHMMSS[.uuuu] format
    var timere = "(\\d{2}):?(\\d{2}):?(\\d{2}(?:\\.\\d+)?)";
    // The timezone as Z or in +HH[:MM] or -HH[:MM] format
    var tzre = "(Z|(?:\\+|-)\\d{2}(?:\\:\\d{2})?)?";
    this.re = new RegExp("^" + datere + "[ T]" + timere + tzre + "$");
  }

  var matches = this.re.exec(datestr);
  if (! matches)
    return null;

  var year = matches[1];
  var month = matches[2] - 1;
  var day = matches[3];
  var hour = matches[4];
  var minute = matches[5];
  var second = Math.floor(matches[6]);
  var ms = matches[6] - second;
  var tz = matches[7];
  var ms = 0;
  var offset = 0;

  if (tz && tz != "Z") {
    var tzmatches = tz.match(/^(\+|-)(\d{2})(\:(\d{2}))$/);
    if (tzmatches) {
      offset = Number(tzmatches[2]) * 60 + Number(tzmatches[4]);
      if (tzmatches[1] == "-")
        offset = -offset;
    }
  }

  offset *= 60 * 1000;
  var dateval = Date.UTC(year, month, day, hour, minute, second, ms) - offset;

  return new Date(dateval);
}

不幸的是,它也没有处理时区缩写。你必须修改tzre表达式来接受字母,我知道在Javascript中处理时区缩写的唯一解决方案是有一个查找表,你可以在更改区域时手动保持最新状态夏令时。

Unfortunately, it doesn't handle timezone abbreviations either. You would have to modify the "tzre" expression to accept letters, and the only solution I know of to deal with timezone abbreviations in Javascript is to have a look-up table which you keep up to date manually in the event of changes to regional daylight savings times.

这篇关于如何在javascript中将datetime微格式转换为本地时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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