moment.js 是否允许我从这个字符串“(GMT-10:00) Hawaii”中导出时区缩写?
Does moment.js allow me to derive a timezone abbreviation from this string "(GMT-10:00) Hawaii"?
我有一个具有 2 个可用属性的对象 - timestamp
和 timezone
,它们通常看起来像这样:
time: {
timestamp: 1576099544000,
timezone: "Africa/Accra"
}
问题是有时看起来像这样:
time: {
timestamp: 1576099544000,
timezone: "(GMT-10:00) Hawaii"
}
所以当尝试这个时:
return moment(time.timestamp).tz(time.timezone).format('YYYY-MM-DD hh:mm:ss z A');
我在第一个示例中返回了这个:
2020-02-24 07:01:46 GMT PM
第二个例子是这样的:
2020-02-24 07:01:46 PM
我的观点是,当时区具有第二种格式时,我不会返回时区缩写(在本例中为 GMT)。会不会是第二时区的格式不对?有什么解决办法吗?
第二种格式的所有可能时区:
{
"timezones":[
{"zone":"(GMT-12:00) International Date Line West", "name":"Dateline Standard Time"},
{"zone":"(GMT-11:00) Midway Island, Samoa", "name":"Samoa Standard Time"},
{"zone":"(GMT-10:00) Hawaii", "name":"Hawaiian Standard Time"},
{"zone":"(GMT-09:00) Alaska", "name":"Alaskan Standard Time"},
{"zone":"(GMT-08:00) Pacific Time (US and Canada); Tijuana", "name":"Pacific Standard Time"},
{"zone":"(GMT-07:00) Mountain Time (US and Canada)", "name":"Mountain Standard Time"},
{"zone":"(GMT-07:00) Chihuahua, La Paz, Mazatlan", "name":"Mexico Standard Time 2"},
{"zone":"(GMT-07:00) Arizona", "name":"U.S. Mountain Standard Time"},
{"zone":"(GMT-06:00) Central Time (US and Canada", "name":"Central Standard Time"},
{"zone":"(GMT-06:00) Saskatchewan", "name":"Canada Central Standard Time"},
{"zone":"(GMT-06:00) Guadalajara, Mexico City, Monterrey", "name":"Mexico Standard Time"},
{"zone":"(GMT-06:00) Central America", "name":"Central America Standard Time"},
{"zone":"(GMT-05:00) Eastern Time (US and Canada)", "name":"Eastern Standard Time"},
{"zone":"(GMT-05:00) Indiana (East)", "name":"U.S. Eastern Standard Time"},
{"zone":"(GMT-05:00) Bogota, Lima, Quito", "name":"S.A. Pacific Standard Time"},
{"zone":"(GMT-04:00) Atlantic Time (Canada)", "name":"Atlantic Standard Time"},
{"zone":"(GMT-04:00) Caracas, La Paz", "name":"S.A. Western Standard Time"},
{"zone":"(GMT-04:00) Santiago", "name":"Pacific S.A. Standard Time"},
{"zone":"(GMT-03:30) Newfoundland and Labrador", "name":"Newfoundland and Labrador Standard Time"},
{"zone":"(GMT-03:00) Brasilia", "name":"E. South America Standard Time"},
{"zone":"(GMT-03:00) Buenos Aires, Georgetown", "name":"S.A. Eastern Standard Time"},
{"zone":"(GMT-03:00) Greenland", "name":"Greenland Standard Time"},
{"zone":"(GMT-02:00) Mid-Atlantic", "name":"Mid-Atlantic Standard Time"},
{"zone":"(GMT-01:00) Azores", "name":"Azores Standard Time"},
{"zone":"(GMT-01:00) Cape Verde Islands", "name":"Cape Verde Standard Time"},
{"zone":"(GMT+00:00) Dublin, Edinburgh, Lisbon, London", "name":"GMT Standard Time"},
{"zone":"(GMT+00:00) Monrovia, Reykjavik ", "name":"Greenwich Standard Time"},
{"zone":"(GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague", "name":"Central Europe Standard Time"},
{"zone":"(GMT+01:00) Sarajevo, Skopje, Warsaw, Zagreb", "name":"Central European Standard Time"},
{"zone":"(GMT+01:00) Brussels, Copenhagen, Madrid, Paris", "name":"Romance Standard Time"},
{"zone":"(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna", "name":"W. Europe Standard Time"},
{"zone":"(GMT+01:00) West Central Africa", "name":"W. Central Africa Standard Time"},
{"zone":"(GMT+02:00) Bucharest", "name":"E. Europe Standard Time"},
{"zone":"(GMT+02:00) Cairo", "name":"Egypt Standard Time"},
{"zone":"(GMT+02:00) Helsinki, Kiev, Riga, Sofia, Tallinn, Vilnius", "name":"FLE Standard Time"},
{"zone":"(GMT+02:00) Athens, Istanbul, Minsk", "name":"GTB Standard Time"},
{"zone":"(GMT+02:00) Jerusalem", "name":"Israel Standard Time"},
{"zone":"(GMT+02:00) Harare, Pretoria", "name":"South Africa Standard Time"},
{"zone":"(GMT+03:00) Moscow, St. Petersburg, Volgograd", "name":"Russian Standard Time"},
{"zone":"(GMT+03:00) Kuwait, Riyadh", "name":"Arab Standard Time"},
{"zone":"(GMT+03:00) Nairobi", "name":"E. Africa Standard Time"},
{"zone":"(GMT+03:00) Baghdad", "name":"Arabic Standard Time"},
{"zone":"(GMT+03:30) Tehran", "name":"Iran Standard Time"},
{"zone":"(GMT+04:00) Abu Dhabi, Muscat", "name":"Arabian Standard Time"},
{"zone":"(GMT+04:00) Baku, Tbilisi, Yerevan", "name":"Caucasus Standard Time"},
{"zone":"(GMT+04:30) Kabul", "name":"Transitional Islamic State of Afghanistan Standard Time"},
{"zone":"(GMT+05:00) Ekaterinburg", "name":"Ekaterinburg Standard Time"},
{"zone":"(GMT+05:00) Islamabad, Karachi, Tashkent", "name":"West Asia Standard Time"},
{"zone":"(GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi", "name":"India Standard Time"},
{"zone":"(GMT+05:45) Kathmandu", "name":"Nepal Standard Time"},
{"zone":"(GMT+06:00) Astana, Dhaka", "name":"Central Asia Standard Time"},
{"zone":"(GMT+06:00) Sri Jayawardenepura", "name":"Sri Lanka Standard Time"},
{"zone":"(GMT+06:00) Almaty, Novosibirsk", "name":"N. Central Asia Standard Time"},
{"zone":"(GMT+06:30) Yangon Rangoon", "name":"Myanmar Standard Time"},
{"zone":"(GMT+07:00) Bangkok, Hanoi, Jakarta", "name":"S.E. Asia Standard Time"},
{"zone":"(GMT+07:00) Krasnoyarsk", "name":"North Asia Standard Time"},
{"zone":"(GMT+08:00) Beijing, Chongqing, Hong Kong SAR, Urumqi", "name":"China Standard Time"},
{"zone":"(GMT+08:00) Kuala Lumpur, Singapore", "name":"Singapore Standard Time"},
{"zone":"(GMT+08:00) Taipei", "name":"Taipei Standard Time"},
{"zone":"(GMT+08:00) Perth", "name":"W. Australia Standard Time"},
{"zone":"(GMT+08:00) Irkutsk, Ulaanbaatar", "name":"North Asia East Standard Time"},
{"zone":"(GMT+09:00) Seoul", "name":"Korea Standard Time"},
{"zone":"(GMT+09:00) Osaka, Sapporo, Tokyo", "name":"Tokyo Standard Time"},
{"zone":"(GMT+09:00) Yakutsk", "name":"Yakutsk Standard Time"},
{"zone":"(GMT+09:30) Darwin", "name":"A.U.S. Central Standard Time"},
{"zone":"(GMT+09:30) Adelaide", "name":"Cen. Australia Standard Time"},
{"zone":"(GMT+10:00) Canberra, Melbourne, Sydney", "name":"A.U.S. Eastern Standard Time"},
{"zone":"(GMT+10:00) Brisbane", "name":"E. Australia Standard Time"},
{"zone":"(GMT+10:00) Hobart", "name":"Tasmania Standard Time"},
{"zone":"(GMT+10:00) Vladivostok", "name":"Vladivostok Standard Time"},
{"zone":"(GMT+10:00) Guam, Port Moresby", "name":"West Pacific Standard Time"},
{"zone":"(GMT+11:00) Magadan, Solomon Islands, New Caledonia", "name":"Central Pacific Standard Time"},
{"zone":"(GMT+12:00) Fiji Islands, Kamchatka, Marshall Islands", "name":"Fiji Islands Standard Time"},
{"zone":"(GMT+12:00) Auckland, Wellington", "name":"New Zealand Standard Time"},
{"zone":"(GMT+13:00) Nuku'alofa", "name":"Tonga Standard Time"}
]
}
一个快速的解决方法是:
检查
time.timezone.substring(0, 4) ==="(GMT"
如果 true
将 GMT
添加到“PM”/“AM”之前的返回值
像这样:
if(time.timezone.substring(0, 4) ==="(GMT"){
let temp = moment(time.timestamp).tz(time.timezone).format('YYYY-MM-DD hh:mm:ss z A');
let result = temp.substring(0, temp.length - 2);
result += "GMT " + temp.substring(temp.length - 2);
return result;
}
return moment(time.timestamp).tz(time.timezone).format('YYYY-MM-DD hh:mm:ss z A');
我有一个具有 2 个可用属性的对象 - timestamp
和 timezone
,它们通常看起来像这样:
time: {
timestamp: 1576099544000,
timezone: "Africa/Accra"
}
问题是有时看起来像这样:
time: {
timestamp: 1576099544000,
timezone: "(GMT-10:00) Hawaii"
}
所以当尝试这个时:
return moment(time.timestamp).tz(time.timezone).format('YYYY-MM-DD hh:mm:ss z A');
我在第一个示例中返回了这个:
2020-02-24 07:01:46 GMT PM
第二个例子是这样的:
2020-02-24 07:01:46 PM
我的观点是,当时区具有第二种格式时,我不会返回时区缩写(在本例中为 GMT)。会不会是第二时区的格式不对?有什么解决办法吗?
第二种格式的所有可能时区:
{
"timezones":[
{"zone":"(GMT-12:00) International Date Line West", "name":"Dateline Standard Time"},
{"zone":"(GMT-11:00) Midway Island, Samoa", "name":"Samoa Standard Time"},
{"zone":"(GMT-10:00) Hawaii", "name":"Hawaiian Standard Time"},
{"zone":"(GMT-09:00) Alaska", "name":"Alaskan Standard Time"},
{"zone":"(GMT-08:00) Pacific Time (US and Canada); Tijuana", "name":"Pacific Standard Time"},
{"zone":"(GMT-07:00) Mountain Time (US and Canada)", "name":"Mountain Standard Time"},
{"zone":"(GMT-07:00) Chihuahua, La Paz, Mazatlan", "name":"Mexico Standard Time 2"},
{"zone":"(GMT-07:00) Arizona", "name":"U.S. Mountain Standard Time"},
{"zone":"(GMT-06:00) Central Time (US and Canada", "name":"Central Standard Time"},
{"zone":"(GMT-06:00) Saskatchewan", "name":"Canada Central Standard Time"},
{"zone":"(GMT-06:00) Guadalajara, Mexico City, Monterrey", "name":"Mexico Standard Time"},
{"zone":"(GMT-06:00) Central America", "name":"Central America Standard Time"},
{"zone":"(GMT-05:00) Eastern Time (US and Canada)", "name":"Eastern Standard Time"},
{"zone":"(GMT-05:00) Indiana (East)", "name":"U.S. Eastern Standard Time"},
{"zone":"(GMT-05:00) Bogota, Lima, Quito", "name":"S.A. Pacific Standard Time"},
{"zone":"(GMT-04:00) Atlantic Time (Canada)", "name":"Atlantic Standard Time"},
{"zone":"(GMT-04:00) Caracas, La Paz", "name":"S.A. Western Standard Time"},
{"zone":"(GMT-04:00) Santiago", "name":"Pacific S.A. Standard Time"},
{"zone":"(GMT-03:30) Newfoundland and Labrador", "name":"Newfoundland and Labrador Standard Time"},
{"zone":"(GMT-03:00) Brasilia", "name":"E. South America Standard Time"},
{"zone":"(GMT-03:00) Buenos Aires, Georgetown", "name":"S.A. Eastern Standard Time"},
{"zone":"(GMT-03:00) Greenland", "name":"Greenland Standard Time"},
{"zone":"(GMT-02:00) Mid-Atlantic", "name":"Mid-Atlantic Standard Time"},
{"zone":"(GMT-01:00) Azores", "name":"Azores Standard Time"},
{"zone":"(GMT-01:00) Cape Verde Islands", "name":"Cape Verde Standard Time"},
{"zone":"(GMT+00:00) Dublin, Edinburgh, Lisbon, London", "name":"GMT Standard Time"},
{"zone":"(GMT+00:00) Monrovia, Reykjavik ", "name":"Greenwich Standard Time"},
{"zone":"(GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague", "name":"Central Europe Standard Time"},
{"zone":"(GMT+01:00) Sarajevo, Skopje, Warsaw, Zagreb", "name":"Central European Standard Time"},
{"zone":"(GMT+01:00) Brussels, Copenhagen, Madrid, Paris", "name":"Romance Standard Time"},
{"zone":"(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna", "name":"W. Europe Standard Time"},
{"zone":"(GMT+01:00) West Central Africa", "name":"W. Central Africa Standard Time"},
{"zone":"(GMT+02:00) Bucharest", "name":"E. Europe Standard Time"},
{"zone":"(GMT+02:00) Cairo", "name":"Egypt Standard Time"},
{"zone":"(GMT+02:00) Helsinki, Kiev, Riga, Sofia, Tallinn, Vilnius", "name":"FLE Standard Time"},
{"zone":"(GMT+02:00) Athens, Istanbul, Minsk", "name":"GTB Standard Time"},
{"zone":"(GMT+02:00) Jerusalem", "name":"Israel Standard Time"},
{"zone":"(GMT+02:00) Harare, Pretoria", "name":"South Africa Standard Time"},
{"zone":"(GMT+03:00) Moscow, St. Petersburg, Volgograd", "name":"Russian Standard Time"},
{"zone":"(GMT+03:00) Kuwait, Riyadh", "name":"Arab Standard Time"},
{"zone":"(GMT+03:00) Nairobi", "name":"E. Africa Standard Time"},
{"zone":"(GMT+03:00) Baghdad", "name":"Arabic Standard Time"},
{"zone":"(GMT+03:30) Tehran", "name":"Iran Standard Time"},
{"zone":"(GMT+04:00) Abu Dhabi, Muscat", "name":"Arabian Standard Time"},
{"zone":"(GMT+04:00) Baku, Tbilisi, Yerevan", "name":"Caucasus Standard Time"},
{"zone":"(GMT+04:30) Kabul", "name":"Transitional Islamic State of Afghanistan Standard Time"},
{"zone":"(GMT+05:00) Ekaterinburg", "name":"Ekaterinburg Standard Time"},
{"zone":"(GMT+05:00) Islamabad, Karachi, Tashkent", "name":"West Asia Standard Time"},
{"zone":"(GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi", "name":"India Standard Time"},
{"zone":"(GMT+05:45) Kathmandu", "name":"Nepal Standard Time"},
{"zone":"(GMT+06:00) Astana, Dhaka", "name":"Central Asia Standard Time"},
{"zone":"(GMT+06:00) Sri Jayawardenepura", "name":"Sri Lanka Standard Time"},
{"zone":"(GMT+06:00) Almaty, Novosibirsk", "name":"N. Central Asia Standard Time"},
{"zone":"(GMT+06:30) Yangon Rangoon", "name":"Myanmar Standard Time"},
{"zone":"(GMT+07:00) Bangkok, Hanoi, Jakarta", "name":"S.E. Asia Standard Time"},
{"zone":"(GMT+07:00) Krasnoyarsk", "name":"North Asia Standard Time"},
{"zone":"(GMT+08:00) Beijing, Chongqing, Hong Kong SAR, Urumqi", "name":"China Standard Time"},
{"zone":"(GMT+08:00) Kuala Lumpur, Singapore", "name":"Singapore Standard Time"},
{"zone":"(GMT+08:00) Taipei", "name":"Taipei Standard Time"},
{"zone":"(GMT+08:00) Perth", "name":"W. Australia Standard Time"},
{"zone":"(GMT+08:00) Irkutsk, Ulaanbaatar", "name":"North Asia East Standard Time"},
{"zone":"(GMT+09:00) Seoul", "name":"Korea Standard Time"},
{"zone":"(GMT+09:00) Osaka, Sapporo, Tokyo", "name":"Tokyo Standard Time"},
{"zone":"(GMT+09:00) Yakutsk", "name":"Yakutsk Standard Time"},
{"zone":"(GMT+09:30) Darwin", "name":"A.U.S. Central Standard Time"},
{"zone":"(GMT+09:30) Adelaide", "name":"Cen. Australia Standard Time"},
{"zone":"(GMT+10:00) Canberra, Melbourne, Sydney", "name":"A.U.S. Eastern Standard Time"},
{"zone":"(GMT+10:00) Brisbane", "name":"E. Australia Standard Time"},
{"zone":"(GMT+10:00) Hobart", "name":"Tasmania Standard Time"},
{"zone":"(GMT+10:00) Vladivostok", "name":"Vladivostok Standard Time"},
{"zone":"(GMT+10:00) Guam, Port Moresby", "name":"West Pacific Standard Time"},
{"zone":"(GMT+11:00) Magadan, Solomon Islands, New Caledonia", "name":"Central Pacific Standard Time"},
{"zone":"(GMT+12:00) Fiji Islands, Kamchatka, Marshall Islands", "name":"Fiji Islands Standard Time"},
{"zone":"(GMT+12:00) Auckland, Wellington", "name":"New Zealand Standard Time"},
{"zone":"(GMT+13:00) Nuku'alofa", "name":"Tonga Standard Time"}
]
}
一个快速的解决方法是: 检查
time.timezone.substring(0, 4) ==="(GMT"
如果 true
将 GMT
添加到“PM”/“AM”之前的返回值
像这样:
if(time.timezone.substring(0, 4) ==="(GMT"){
let temp = moment(time.timestamp).tz(time.timezone).format('YYYY-MM-DD hh:mm:ss z A');
let result = temp.substring(0, temp.length - 2);
result += "GMT " + temp.substring(temp.length - 2);
return result;
}
return moment(time.timestamp).tz(time.timezone).format('YYYY-MM-DD hh:mm:ss z A');