admin管理员组文章数量:1389749
I need to trigger time based events on my server which runs in UTC time.
In my UI, I am accepting 2 parameters
- The local time the trigger that needs to be run
- 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.
IS there an API which returns, say
moment.tz.name('330') = 'Asia/Kolkata'
Also, if there is a solution, would DST problem be addressed
I need to trigger time based events on my server which runs in UTC time.
In my UI, I am accepting 2 parameters
- The local time the trigger that needs to be run
- 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.
IS there an API which returns, say
moment.tz.name('330') = 'Asia/Kolkata'
Also, if there is a solution, would DST problem be addressed
- 2 Did the information that you need is in 'new Date()' (for me it's give: Thu Feb 23 2017 16:54:19 GMT+0100 (Paris, Madrid)). If yes, I can make you a REGEX to extract them. – Alexandre Nicolas Commented Feb 23, 2017 at 15:56
- 1 example.. you would give a custom time, say 17h and 00 minutes, to run an event. Additionally, you would send +1 as offeset.. The server is in UTC timezone. It has to run event at 17:00:00 GMT +0100 – Faiz Mohamed Haneef Commented Feb 23, 2017 at 15:58
- 1 So you just need the offset ? the +1 – Alexandre Nicolas Commented Feb 23, 2017 at 16:01
- 2 No, there is no API that returns timezone name from offset, I fear that is not possible to get this kind of output from offset, see here. – VincenzoC Commented Feb 23, 2017 at 16:03
- 1 The client should send a date in UTC it will be easier. So if the user select 17:00 (+1 GMT) it send 18:00 – Alexandre Nicolas Commented Feb 23, 2017 at 16:05
2 Answers
Reset to default 3So 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.
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. You can also get an array of all the timezone names with their offsets like with a simple map on 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.
When working with fixed offsets, you do not need to use Moment-Timezone. All of that functionality is provided with Moment itself, with either the parseZone
or utcOffset
functions.
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"
本文标签: momentjsGetting timezone from offset in javascriptStack Overflow
版权声明:本文标题:momentjs - Getting timezone from offset in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744742324a2622680.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论