从时区标识符获取当前 GMT 偏移量
Get current GMT offset from a timezone identifier
如何根据时区标识符获取当前 GMT 偏移量?理想情况下,它也将包括长格式名称。
例如:
"America/Los_Angeles" //output: GMT-0700 (Pacific Daylight Time)
如果它也能与 ISO 字符串一起使用就更好了,例如:
2020-12-21T03:57:00Z //output: GMT-0800 (Pacific Standard Time)
您可以使用 Intl.DateTimeFormat 对象的 timeZone 和 timeZoneName 选项来获取更常见的名称时区,但鲜为人知的时区可能会丢失。还有:
- 你不能在同一个调用中得到它们,所以你需要调用它两次
- 在某些情况下,您只会得到短名称和长名称,而没有实际的偏移量
- 时区名称未标准化,因此不同的实现可能 return 不同的名称,或者只是没有名称的实际偏移量。
- 您将获得您创建的日期和时间的偏移量,而不是位置的日期和时间,因此如果该差异跨越夏令时边界,则可能是错误的
例如
// Get short offset, might show the actual offset but might be a short name
let formatterA = new Intl.DateTimeFormat('en',{timeZone:'America/New_York', timeZoneName:'short'});
console.log( formatterA.format(new Date()) ); // 5/2/2020, EDT
// Get short offset, might show the actual offset but might be a short name
let formatterB = new Intl.DateTimeFormat('en',{timeZone:'America/New_York', timeZoneName:'long'});
console.log( formatterB.format(new Date()) ); // 5/2/2020, Eastern Daylight Time
另一种获取偏移量的策略是在时区中生成一个日期,并通过解析结果来获取与具有相同年月日等值的UTC日期的差异。它仍然存在夏令时边界问题。 Intl.DateTimeFormat.prototype.formatToParts method helps as for this answer.
但是,我建议您使用像 Luxon 这样的库,因为弄乱这些东西可能会让您头疼,尤其是关于夏令时变化的部分。
var DateTime = luxon.DateTime;
let d = DateTime.fromISO("2017-05-15T09:10:23", { zone: "Europe/Paris" });
console.log(d.toFormat('ZZ')); // +02:00
console.log(d.toFormat('ZZZZZ')); // Central European Summer Time
let e = DateTime.fromISO("2017-05-15T09:10:23", { zone: "Pacific/Kiritimati" });
console.log(e.toFormat('ZZ')); // +14:00
console.log(e.toFormat('ZZZZZ')); // Line Islands Time
<script src="https://cdn.jsdelivr.net/npm/luxon@1.23.0/build/global/luxon.min.js"></script>
如何根据时区标识符获取当前 GMT 偏移量?理想情况下,它也将包括长格式名称。
例如:
"America/Los_Angeles" //output: GMT-0700 (Pacific Daylight Time)
如果它也能与 ISO 字符串一起使用就更好了,例如:
2020-12-21T03:57:00Z //output: GMT-0800 (Pacific Standard Time)
您可以使用 Intl.DateTimeFormat 对象的 timeZone 和 timeZoneName 选项来获取更常见的名称时区,但鲜为人知的时区可能会丢失。还有:
- 你不能在同一个调用中得到它们,所以你需要调用它两次
- 在某些情况下,您只会得到短名称和长名称,而没有实际的偏移量
- 时区名称未标准化,因此不同的实现可能 return 不同的名称,或者只是没有名称的实际偏移量。
- 您将获得您创建的日期和时间的偏移量,而不是位置的日期和时间,因此如果该差异跨越夏令时边界,则可能是错误的
例如
// Get short offset, might show the actual offset but might be a short name
let formatterA = new Intl.DateTimeFormat('en',{timeZone:'America/New_York', timeZoneName:'short'});
console.log( formatterA.format(new Date()) ); // 5/2/2020, EDT
// Get short offset, might show the actual offset but might be a short name
let formatterB = new Intl.DateTimeFormat('en',{timeZone:'America/New_York', timeZoneName:'long'});
console.log( formatterB.format(new Date()) ); // 5/2/2020, Eastern Daylight Time
另一种获取偏移量的策略是在时区中生成一个日期,并通过解析结果来获取与具有相同年月日等值的UTC日期的差异。它仍然存在夏令时边界问题。 Intl.DateTimeFormat.prototype.formatToParts method helps as for this answer.
但是,我建议您使用像 Luxon 这样的库,因为弄乱这些东西可能会让您头疼,尤其是关于夏令时变化的部分。
var DateTime = luxon.DateTime;
let d = DateTime.fromISO("2017-05-15T09:10:23", { zone: "Europe/Paris" });
console.log(d.toFormat('ZZ')); // +02:00
console.log(d.toFormat('ZZZZZ')); // Central European Summer Time
let e = DateTime.fromISO("2017-05-15T09:10:23", { zone: "Pacific/Kiritimati" });
console.log(e.toFormat('ZZ')); // +14:00
console.log(e.toFormat('ZZZZZ')); // Line Islands Time
<script src="https://cdn.jsdelivr.net/npm/luxon@1.23.0/build/global/luxon.min.js"></script>