首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用moment-timezone获取所有可用时区的列表

如何使用moment-timezone获取所有可用时区的列表
EN

Stack Overflow用户
提问于 2016-06-23 20:03:59
回答 3查看 16K关注 0票数 23

我正在尝试使用node js中的moment-timezone来获取所有可用时区的列表,如下所示-

代码语言:javascript
复制
var moment = require('moment-timezone');
var timeZones = moment.tz.names();
console.log(timeZones);

我正在获取这种格式的时区-

代码语言:javascript
复制
'Europe/Mariehamn',
'Europe/Minsk',
'Europe/Monaco',
'Europe/Moscow',
'Europe/Nicosia',
'Europe/Oslo',
'Europe/Paris',
'Europe/Podgorica',
'Europe/Prague',
'Europe/Riga',
'Europe/Rome',

但我想得到这种格式的时区-

代码语言:javascript
复制
(GMT +01:00) Africa/Brazzaville
(GMT +01:00) Africa/Casablanca
(GMT +01:00) Africa/Douala
(GMT +01:00) Africa/El_Aaiun
(GMT +01:00) Africa/Kinshasa
(GMT +01:00) Africa/Lagos
(GMT +01:00) Africa/Libreville
(GMT +01:00) Africa/Luanda
(GMT +01:00) Africa/Malabo
(GMT +01:00) Africa/Ndjamena
(GMT +01:00) Africa/Niamey

我该怎么做呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-06-24 20:03:33

没有直接从moment-timezone获取所需格式的直接方法。

尝试如下所示。

代码语言:javascript
复制
var moment = require('moment-timezone');
var timeZones = moment.tz.names();
var offsetTmz=[];

for(var i in timeZones)
{
    offsetTmz.push(" (GMT"+moment.tz(timeZones[i]).format('Z')+") " + timeZones[i]);
}

现在,offsetTmz是所需格式的字符串数组。

这就是我使用它的方式。

希望这能对你有所帮助。

票数 29
EN

Stack Overflow用户

发布于 2018-06-17 20:34:30

基于@Shrabanee的回答和@Tenz的评论-这是我的解决方案,使用es6模板文字,并根据格林威治标准时间+编号而不是时区名称对列表进行排序:

代码语言:javascript
复制
    timeZones = momentTimezone.tz.names();
    let offsetTmz=[];

    for(let i in timeZones)
    {
        offsetTmz.push(`(GMT${moment.tz(timeZones[i]).format('Z')}) ${timeZones[i]}`);
    }

    this.timeZoneNames = offsetTmz.sort();
票数 6
EN

Stack Overflow用户

发布于 2019-02-26 20:17:51

基于the answer by Erez Libermanthe answer by Matt Johnson about trimming the list,我想添加我的类作为一个完整的Typescript类,它以相反的顺序对带有负偏移量的时区进行排序

代码语言:javascript
复制
import * as moment from 'moment-timezone';

export class TimezoneData {
    tzName: string;
    tzPresentationName: string;
}

export class TimezoneUtils {

    public static getTimezonesNames(): TimezoneData[] {
        const arr: TimezoneData[] = [];
        const names = moment.tz.names();
        for (const name of names) {
            if ((name.indexOf('/') < 0 && name !== 'UTC') || name.startsWith('Etc/')) {
                continue;
            }
            const data = new TimezoneData();
            data.tzName = name;
            data.tzPresentationName = moment.tz(name).format('Z');
            arr.push(data);
        }
        arr.sort((a, b) => {
            if (a.tzPresentationName === b.tzPresentationName) {
                if (a.tzName === 'UTC') {
                    return -1;
                }
                return a.tzName === b.tzName ? 0 : (a.tzName > b.tzName ? 1 : -1);
            }
            const afc = a.tzPresentationName.charAt(0);
            const bfc = b.tzPresentationName.charAt(0);
            if (afc === '-') {
                if (bfc === '+') {
                    return -1;
                }
                return a.tzPresentationName > b.tzPresentationName ? -1 : 1;
            }
            if (bfc === '-') {
                return 1;
            }
            return a.tzPresentationName > b.tzPresentationName ? 1 : -1;
        });
        arr.forEach(a => a.tzPresentationName = `${a.tzName} (GMT ${a.tzPresentationName})`);
        return arr;
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37991113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档