简体   繁体   English

如何在 Javascript 中将带有 3 个字母时区缩写的日期转换为 UTC?

[英]How to convert dates with 3 letter timezone abbreviation, to UTC, in Javascript?

I need to convert datetimes from an input format which I can't change (this: "Tue, 30 Jul 2019 21:15:53 GMT") to UTC, in Javascript.我需要在 Javascript 中将日期时间从我无法更改的输入格式(此:“星期二,2019 年 7 月 30 日 21:15:53 GMT”)转换为 UTC。

I actually need to get these dates as the number of milliseconds since the Unix epoch (1970) but getting in UTC would be a start.我实际上需要将这些日期作为自 Unix 时代(1970 年)以来的毫秒数,但进入 UTC 将是一个开始。

Is there a way to do this easily?有没有办法轻松做到这一点? I can use a 3rd party library if needed.如果需要,我可以使用第 3 方库。 I've heard of moment-timezone.js but not clear how to parse the 3 letter timezone, ie these: https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations .我听说过 moment-timezone.js 但不清楚如何解析 3 个字母的时区,即这些: https : //en.wikipedia.org/wiki/List_of_time_zone_abbreviations

The correct solution is a library that maps these abbreviations to offsets from GMT.正确的解决方案是将这些缩写映射到 GMT 偏移量的库。 Neither moment-timezone , nor date-fns-tz , nor luxon , nor timezone-support do this, but timezone-abbr-offsets does and is very minimalistic . moment-timezonedate-fns-tz fns date-fns-tzluxontimezone-support都不会这样做,但是timezone-abbr-offsets会并且非常简约

Fortunately, new Date() can parse your format minus the timezone, so we'll split that away and calculate the offset back:幸运的是, new Date()可以解析您的格式减去时区,所以我们将把它分开并计算偏移量:

import timezones from 'timezone-abbr-offsets';

function abbrTzToUtc(dateString) {
  // Get the date and the timezone from the input string
  let [, date, tz] = dateString.match(/^(.*)\s+(\w+)$/);
  // Ignore the timezone and parse the date as GMT
  date = new Date(date + 'Z');
  // Add the offset caused by the original timezone
  date = new Date(date.getTime() + timezones[tz] * 60 * 1000);
  return date;
}

console.log(abbrTzToUtc('Tue, 30 Jul 2019 21:15:53 MET'));

As a test, the code above should return 2019-07-30T22:15:53.000Z .作为测试,上面的代码应该返回2019-07-30T22:15:53.000Z

If you want number of milliseconds since the Unix epoch, return date.getTime() instead.如果您想要自 Unix 纪元以来的毫秒数,请return date.getTime()

If you want to convert the date into UTC format you can use toISOString()如果要将日期转换为 UTC 格式,可以使用 toISOString()

new Date('Tue, 30 Jul 2019 21:15:53 GMT').toISOString()

For more info please check this reference有关更多信息,请查看此参考

Moreover, to convert the date into milliseconds you can use Date.UTC()此外,要将日期转换为毫秒,您可以使用 Date.UTC()

Date.UTC(year[, month[, day[, hour[, minute[, second[, millisecond]]]]]])

reference 参考

Example:例子:

utcMillisecond = (e) => {
    const regex = /(T)|(:)|(-)/g;
    const utc = new Date(e).toISOString().slice(0, 19).replace(regex, ' ').split(' ');
    const utcMillisecond = Date.UTC(utc[0], utc[1], utc[2], utc[3], utc[4])
    return utcMillisecond
}

utcMillisecond("Tue, 30 Jul 2019 21:15:53 GMT")
//1567199700000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM