如何使用时刻从时区标准名称找到时间
我的时区信息如下所示。
{"timezone_standard_name": "(UTC-06:00) Central Time (US & Canada)"}.
是否可以找到上述时区的日期和时间。
我已经尝试找到UTC时间,然后添加偏移量,但它只会
在offset
添加偏移
尝试过。我们有更好的答案吗? 使用字符串操纵(UTC-06:00)查找+360的偏移量
const tzNames = momenttz.tz.names();
const map = new Map();
for (const name of tzNames) {
const offsets = momenttz.tz.zone(name).offsets;
for (const offset of offsets) {
if (!map.has(offset)) {
map.set(offset, new Set());
}
map.get(offset).add(name);
}
}
const offsetList = map.get(360);
for (const item of offsetList) {
console.log(item);
let tx = momenttz().tz(item).format();
console.log('Time in given zone is = ' + tx);
}
I have the time zone info as shown in below JSON.
{"timezone_standard_name": "(UTC-06:00) Central Time (US & Canada)"}.
Is it possible to find the date and time for the above time zone.
I have already tried to find UTC time then add the offset but it will just give me the date and time object in IST
Before offset
After adding offset
This is what I have tried. Do we have any better answer?
Find the offset that is +360 using string manipulation (UTC-06:00)
const tzNames = momenttz.tz.names();
const map = new Map();
for (const name of tzNames) {
const offsets = momenttz.tz.zone(name).offsets;
for (const offset of offsets) {
if (!map.has(offset)) {
map.set(offset, new Set());
}
map.get(offset).add(name);
}
}
const offsetList = map.get(360);
for (const item of offsetList) {
console.log(item);
let tx = momenttz().tz(item).format();
console.log('Time in given zone is = ' + tx);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
时刻timezone
,我们可以调用<代码> Moment.TZ(TimeZone)在该时区中获取时间。但是,时刻需要一个iana timezone
。我们可以使用软件包
windows-windows-iana
,i 've创建了一个函数findianatimezone()
,该为Windows时区名称找到了相应的IANA时区。在这种情况下,我们将在下面的示例中找到等效的IANA时区,然后使用Moment.TZ()在该时区中获取当前时间。
注意:我们不能简单地将UTC偏移添加到一个时区中,因为由于节省日光,这将因一年中的时间而有所不同。另外,一旦我们拥有一个IANA时区,我们就可以使用任何库来获取该区域的当前时间:Moment.JS,Luxon等。
You can get the time in another timezone using
Moment Timezone
, we can callmoment.tz(timezone)
to get the time in that timezone. However moment requires anIANA timezone
and you have a Windows timezone name.We can convert using the package
windows-iana
, I've created a functionfindIANATimezone()
that finds the corresponding IANA timezone for a Windows timezone name.In this case, we'll find the equivalent IANA timezone for the examples below, then use moment.tz() to get the current time in that timezone.
Note: We can't simply add the UTC offset for a timezone, since this will vary by the time of year, due to Daylight Saving Time. Also, once we have an IANA timezone, we can use any library to get the current time in that zone: moment.js, luxon etc.