简体   繁体   English

从javascript中的偏移量获取时区

[英]Getting timezone from offset in javascript

I need to trigger time based events on my server which runs in UTC time.我需要在以 UTC 时间运行的服务器上触发基于时间的事件。
In my UI, I am accepting 2 parameters在我的用户界面中,我接受 2 个参数

  1. The local time the trigger that needs to be run需要运行的触发器的当地时间
  2. The timezone offset时区偏移

I preferred to not show timezone names because it delays the page rendering and I believe its unneeded.我更喜欢不显示时区名称,因为它会延迟页面渲染,而且我认为这是不需要的。

I checked the moment library and I don't see how to get the timezone name from timezone offset.我检查了时刻库,但看不到如何从时区偏移量中获取时区名称。 The API moment.tz.names() returns all timezones. API moment.tz.names()返回所有时区。

IS there an API which returns, say moment.tz.name('330') = 'Asia/Kolkata' Also, if there is a solution, would DST problem be addressed是否有返回的 API,例如moment.tz.name('330') = 'Asia/Kolkata'另外,如果有解决方案,是否可以解决 DST 问题

When working with fixed offsets, you do not need to use Moment-Timezone.使用固定偏移量时,您不需要使用 Moment-Timezone。 All of that functionality is provided with Moment itself, with either the parseZone or utcOffset functions.所有这些功能parseZone Moment 本身提供,使用parseZoneutcOffset函数。

There's an example in the documentation that matches your scenario well.文档中有一个与您的场景非常匹配的示例。

... ...

One use of this feature is if you want to construct a moment with a specific time zone offset using only numeric input values:此功能的一个用途是,如果您想仅使用数字输入值构建具有特定时区偏移量的时刻:

 moment([2016, 0, 1, 0, 0, 0]).utcOffset(-5, true) // Equivalent to "2016-01-01T00:00:00-05:00"

So there will always/often be multiple names for any particular offset so there is no moment.tz.name('offset') = 'Country/Zone' api that I know of.所以对于任何特定的偏移量总是/经常有多个名称,所以没有我知道的moment.tz.name('offset') = 'Country/Zone' api。

You can however get an array of all names based on an an offset with a simple filter function但是,您可以使用简单的过滤器函数根据偏移量获取所有名称的数组

const moment = require('moment-timezone');

const getZoneFromOffset = offsetString => moment.tz.names().filter(tz => moment.tz(tz).format('Z') === offsetString)

console.log(getZoneFromOffset("+05:30"))
// [ 'Asia/Calcutta', 'Asia/Colombo', 'Asia/Kolkata' ]

In the example above I used "+05:30" for the offset value which doesn't 100% match you use case of 330 but it demonstrates how to work the problem.在上面的示例中,我使用"+05:30"作为偏移值,该值与 330 的用例不 100% 匹配,但它演示了如何解决问题。 You can also get an array of all the timezone names with their offsets like with a simple map on moment.tz.names()您还可以获取所有时区名称及其偏移量的数组,就像使用 moment.tz.names() 上的简单地图一样

const getZoneObjects = () =>  moment.tz.names().map(zone => ({ zone, offset: moment.tz(zone).format('Z') }))

/*
[
  { zone: 'Africa/Abidjan', offset: '+00:00' },
  { zone: 'Africa/Accra', offset: '+00:00' },
  { zone: 'Africa/Addis_Ababa', offset: '+03:00' },
  ...
*/

The examples above use Z as the timezone format but if "+5:30" doesn't match your needs you can look at https://devhints.io/moment to see the other formats you can use for example ZZ which would be "+0530" in your use case.上面的示例使用Z作为时区格式,但如果"+5:30"不符合您的需求,您可以查看https://devhints.io/moment以查看您可以使用的其他格式,例如ZZ "+0530"在您的用例中。

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

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