从时区缩写中获取 UTC 偏移量 [英] Get UTC offset from timezone abbreviations

查看:112
本文介绍了从时区缩写中获取 UTC 偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何可以从时区缩写中获得 utc 偏移量?如:

Is there anyway to get utc offset from a timezone abbreviations? Such as:

太平洋标准时间"

为了

太平洋标准时间(北美)

Pacific Standard Time (North America)

这将导致 UTC-08.我在时刻时区找到了一种方法,但它没有多大帮助.它提供了一个时区标识符列表,但其中大部分已被弃用.谢谢.

which will result in UTC−08. I found a way on moment-timezone but it did not help much. It provides a list of timezone identifier but most of them are deprecated. Thank you.

推荐答案

这是一个相当棘手的话题,类似的问题已经讨论过了.

This is a quite tricky topic and similar questions have already been discussed.

请参阅使用 JavaScript 检测时区缩写使用偏移值获取时区缩写

我会提出两个选项:

1.哈希表.根据标准信息(例如时区缩写列表及其偏移量)创建您需要的键值对.类似的东西

1. Hash table. Create a key-value pair of what you require based on standard information (e.g. list of timezone abbreviations and their offset). Something like

    let offsetByAbbrev = {"PST" : "UTC-08", "EST" : "UTC+01", ... }

但这需要认真维护,因为它会很快被弃用.

But this needs serious maintenance as it will get deprecated quickly.

2.日期操作.您可以使用 momentjs 在特定时区创建日期,但不能使用缩写.并且有一个很好的理由:这些缩写可能会改变,也可能导致歧义.你可以查看momentjs-timezone使用的JSON:https://github.com/moment/moment-timezone/blob/develop/data/packed/latest.json 这将使您大致了解它的混乱程度.例如做一个简单的 ctrl+f 搜索 PST 并查看结果.

2. Date manipulation. You can create a date in a specific timezone with momentjs, however, not with abbreviations. And there is a good reason for it: these abbreviations can change and can also cause ambiguity. You can check out the JSON used by momentjs-timezone: https://github.com/moment/moment-timezone/blob/develop/data/packed/latest.json which will give you a general sense of how chaotic it can be. For example do a simple ctrl+f search for PST and see the results.

所以我建议使用时区名称 - 您实际上可以创建和使用 momentjs-timezone.请参阅如何在特定时间创建时间使用 moment.js 的时区,您可以根据时区名称创建一个 moment 对象,该对象将包含(根据 moment 时区的规范:https://momentjs.com/timezone/docs/#/zone-object/) 偏移量本身.

So I would suggest using the timezone names - which you can actually create and use with momentjs-timezone. See How to create time in a specific time zone with moment.js where you can create a moment object based on the timezone name and that object will contain (as per the spec of moment timezone: https://momentjs.com/timezone/docs/#/zone-object/) the offset itself.

我知道这并不能完全解决您的问题,尽管您尝试做的不一定是达到理想解决方案的最佳方法.

I know this does not exactly resolve your problem, though what you are trying to do is not necessarily the best approach to reach your an ideal solution.

这篇关于从时区缩写中获取 UTC 偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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