如何使用moment js获取基于UTC时间的当前时间
How to get the current time based of a UTC time using moment js
我有以下填充世界时区的数组列表
如何根据用户选择的内容获取实际时间。
如果用户选择第一个选项(UTC-12:00) International Date Line West我如何获得当前时间?
您可以使用 Moment Timezone
, we can call moment.tz(timezone)
to get the time in that timezone. However moment requires an IANA timezone
获取另一个时区的时间,并且您有一个包含 Windows 个时区的列表。
我们可以使用包 windows-iana
进行转换,我创建了一个函数 findIANATimezone()
来为 Windows 时区找到相应的 IANA 时区。
在这种情况下,我们将为下面的示例找到等效的 IANA 时区,然后使用 moment.tz() 获取该时区的当前时间。
注意: 我们不能简单地为时区添加 UTC 偏移量,因为这会随时间变化。
// Some examples...
let timezones = [
{
"id": "Dateline Standard Time",
"name": "(UTC-12:00) International Date Line West",
},
{
"id": "Pacific Standard Time",
"name": "(UTC-08:00) Pacific Time (US & Canada)",
},
{
"id": "W. Europe Standard Time",
"name": "(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna",
},
]
console.log(`Timezone Current Time`);
for(let timezone of timezones) {
console.log(timezone.id.padEnd(26, ' ') + getCurrentTime(timezone.id).format('YYYY-MM-DD HH:mm'))
}
function getCurrentTime(timezoneId) {
let ianaTimezone = findIANATimezone(timezoneId);
if (!ianaTimezone) {
return null;
}
return moment.tz(ianaTimezone);
}
function findIANATimezone(id) {
const timeZones = [
{ id: 'Dateline Standard Time', iana: 'Etc/GMT+12' },
{ id: 'Samoa Standard Time', iana: 'Pacific/Apia' },
{ id: 'Hawaiian Standard Time', iana: 'Pacific/Honolulu' },
{ id: 'Alaskan Standard Time', iana: 'America/Anchorage' },
{ id: 'Pacific Standard Time', iana: 'America/Los_Angeles' },
{ id: 'Pacific Standard Time (Mexico)', iana: 'America/Tijuana' },
{ id: 'US Mountain Standard Time', iana: 'America/Phoenix' },
{ id: 'Mountain Standard Time (Mexico)', iana: 'America/Chihuahua' },
{ id: 'Mountain Standard Time', iana: 'America/Denver' },
{ id: 'Central America Standard Time', iana: 'America/Guatemala' },
{ id: 'Central Standard Time', iana: 'America/Chicago' },
{ id: 'Central Standard Time (Mexico)', iana: 'America/Mexico_City' },
{ id: 'Canada Central Standard Time', iana: 'America/Regina' },
{ id: 'SA Pacific Standard Time', iana: 'America/Bogota' },
{ id: 'Eastern Standard Time', iana: 'America/New_York' },
{ id: 'US Eastern Standard Time', iana: 'America/Indianapolis' },
{ id: 'Venezuela Standard Time', iana: 'America/Caracas' },
{ id: 'Atlantic Standard Time', iana: 'America/Halifax' },
{ id: 'SA Western Standard Time', iana: 'America/La_Paz' },
{ id: 'Central Brazilian Standard Time', iana: 'America/Cuiaba' },
{ id: 'Pacific SA Standard Time', iana: 'America/Santiago' },
{ id: 'Newfoundland Standard Time', iana: 'America/St_Johns' },
{ id: 'E. South America Standard Time', iana: 'America/Sao_Paulo' },
{ id: 'Argentina Standard Time', iana: 'America/Buenos_Aires' },
{ id: 'SA Eastern Standard Time', iana: 'America/Cayenne' },
{ id: 'Greenland Standard Time', iana: 'America/Godthab' },
{ id: 'Montevideo Standard Time', iana: 'America/Montevideo' },
{ id: 'Azores Standard Time', iana: 'Atlantic/Azores' },
{ id: 'Cape Verde Standard Time', iana: 'Atlantic/Cape_Verde' },
{ id: 'Morocco Standard Time', iana: 'Africa/Casablanca' },
{ id: 'GMT Standard Time', iana: 'Europe/London' },
{ id: 'Greenwich Standard Time', iana: 'Atlantic/Reykjavik' },
{ id: 'UTC', iana: 'UTC' },
{ id: 'W. Europe Standard Time', iana: 'Europe/Berlin' },
{ id: 'Central Europe Standard Time', iana: 'Europe/Budapest' },
{ id: 'Romance Standard Time', iana: 'Europe/Paris' },
{ id: 'Central European Standard Time', iana: 'Europe/Warsaw' },
{ id: 'W. Central Africa Standard Time', iana: 'Africa/Lagos' },
{ id: 'Jordan Standard Time', iana: 'Asia/Amman' },
{ id: 'GTB Standard Time', iana: 'Europe/Bucharest' },
{ id: 'Middle East Standard Time', iana: 'Asia/Beirut' },
{ id: 'Egypt Standard Time', iana: 'Africa/Cairo' },
{ id: 'South Africa Standard Time', iana: 'Africa/Johannesburg' },
{ id: 'FLE Standard Time', iana: 'Europe/Kiev' },
{ id: 'Israel Standard Time', iana: 'Asia/Jerusalem' },
{ id: 'E. Europe Standard Time', iana: 'Europe/Chisinau' },
{ id: 'Namibia Standard Time', iana: 'Africa/Windhoek' },
{ id: 'Arabic Standard Time', iana: 'Asia/Baghdad' },
{ id: 'Arab Standard Time', iana: 'Asia/Riyadh' },
{ id: 'Russian Standard Time', iana: 'Europe/Moscow' },
{ id: 'E. Africa Standard Time', iana: 'Africa/Nairobi' },
{ id: 'Georgian Standard Time', iana: 'Asia/Tbilisi' },
{ id: 'Iran Standard Time', iana: 'Asia/Tehran' },
{ id: 'Arabian Standard Time', iana: 'Asia/Dubai' },
{ id: 'Azerbaijan Standard Time', iana: 'Asia/Baku' },
{ id: 'Mauritius Standard Time', iana: 'Indian/Mauritius' },
{ id: 'Caucasus Standard Time', iana: 'Asia/Yerevan' },
{ id: 'Afghanistan Standard Time', iana: 'Asia/Kabul' },
{ id: 'Ekaterinburg Standard Time', iana: 'Asia/Yekaterinburg' },
{ id: 'Pakistan Standard Time', iana: 'Asia/Karachi' },
{ id: 'West Asia Standard Time', iana: 'Asia/Tashkent' },
{ id: 'India Standard Time', iana: 'Asia/Calcutta' },
{ id: 'Sri Lanka Standard Time', iana: 'Asia/Colombo' },
{ id: 'Nepal Standard Time', iana: 'Asia/Katmandu' },
{ id: 'N. Central Asia Standard Time', iana: 'Asia/Novosibirsk' },
{ id: 'Central Asia Standard Time', iana: 'Asia/Almaty' },
{ id: 'Myanmar Standard Time', iana: 'Asia/Rangoon' },
{ id: 'SE Asia Standard Time', iana: 'Asia/Bangkok' },
{ id: 'North Asia Standard Time', iana: 'Asia/Krasnoyarsk' },
{ id: 'China Standard Time', iana: 'Asia/Shanghai' },
{ id: 'North Asia East Standard Time', iana: 'Asia/Irkutsk' },
{ id: 'Singapore Standard Time', iana: 'Asia/Singapore' },
{ id: 'W. Australia Standard Time', iana: 'Australia/Perth' },
{ id: 'Taipei Standard Time', iana: 'Asia/Taipei' },
{ id: 'Tokyo Standard Time', iana: 'Asia/Tokyo' },
{ id: 'Korea Standard Time', iana: 'Asia/Seoul' },
{ id: 'Yakutsk Standard Time', iana: 'Asia/Yakutsk' },
{ id: 'Cen. Australia Standard Time', iana: 'Australia/Adelaide' },
{ id: 'AUS Central Standard Time', iana: 'Australia/Darwin' },
{ id: 'E. Australia Standard Time', iana: 'Australia/Brisbane' },
{ id: 'AUS Eastern Standard Time', iana: 'Australia/Sydney' },
{ id: 'West Pacific Standard Time', iana: 'Pacific/Port_Moresby' },
{ id: 'Tasmania Standard Time', iana: 'Australia/Hobart' },
{ id: 'Vladivostok Standard Time', iana: 'Asia/Vladivostok' },
{ id: 'Central Pacific Standard Time', iana: 'Pacific/Guadalcanal' },
{ id: 'New Zealand Standard Time', iana: 'Pacific/Auckland' },
{ id: 'Fiji Standard Time', iana: 'Pacific/Fiji' },
{ id: 'Tonga Standard Time', iana: 'Pacific/Tongatapu' }
]
const tz = timeZones.find(tz => tz.id === id);
return tz ? tz.iana: null;
}
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data-1970-2030.min.js"></script>
我有以下填充世界时区的数组列表
如何根据用户选择的内容获取实际时间。
如果用户选择第一个选项(UTC-12:00) International Date Line West我如何获得当前时间?
您可以使用 Moment Timezone
, we can call moment.tz(timezone)
to get the time in that timezone. However moment requires an IANA timezone
获取另一个时区的时间,并且您有一个包含 Windows 个时区的列表。
我们可以使用包 windows-iana
进行转换,我创建了一个函数 findIANATimezone()
来为 Windows 时区找到相应的 IANA 时区。
在这种情况下,我们将为下面的示例找到等效的 IANA 时区,然后使用 moment.tz() 获取该时区的当前时间。
注意: 我们不能简单地为时区添加 UTC 偏移量,因为这会随时间变化。
// Some examples...
let timezones = [
{
"id": "Dateline Standard Time",
"name": "(UTC-12:00) International Date Line West",
},
{
"id": "Pacific Standard Time",
"name": "(UTC-08:00) Pacific Time (US & Canada)",
},
{
"id": "W. Europe Standard Time",
"name": "(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna",
},
]
console.log(`Timezone Current Time`);
for(let timezone of timezones) {
console.log(timezone.id.padEnd(26, ' ') + getCurrentTime(timezone.id).format('YYYY-MM-DD HH:mm'))
}
function getCurrentTime(timezoneId) {
let ianaTimezone = findIANATimezone(timezoneId);
if (!ianaTimezone) {
return null;
}
return moment.tz(ianaTimezone);
}
function findIANATimezone(id) {
const timeZones = [
{ id: 'Dateline Standard Time', iana: 'Etc/GMT+12' },
{ id: 'Samoa Standard Time', iana: 'Pacific/Apia' },
{ id: 'Hawaiian Standard Time', iana: 'Pacific/Honolulu' },
{ id: 'Alaskan Standard Time', iana: 'America/Anchorage' },
{ id: 'Pacific Standard Time', iana: 'America/Los_Angeles' },
{ id: 'Pacific Standard Time (Mexico)', iana: 'America/Tijuana' },
{ id: 'US Mountain Standard Time', iana: 'America/Phoenix' },
{ id: 'Mountain Standard Time (Mexico)', iana: 'America/Chihuahua' },
{ id: 'Mountain Standard Time', iana: 'America/Denver' },
{ id: 'Central America Standard Time', iana: 'America/Guatemala' },
{ id: 'Central Standard Time', iana: 'America/Chicago' },
{ id: 'Central Standard Time (Mexico)', iana: 'America/Mexico_City' },
{ id: 'Canada Central Standard Time', iana: 'America/Regina' },
{ id: 'SA Pacific Standard Time', iana: 'America/Bogota' },
{ id: 'Eastern Standard Time', iana: 'America/New_York' },
{ id: 'US Eastern Standard Time', iana: 'America/Indianapolis' },
{ id: 'Venezuela Standard Time', iana: 'America/Caracas' },
{ id: 'Atlantic Standard Time', iana: 'America/Halifax' },
{ id: 'SA Western Standard Time', iana: 'America/La_Paz' },
{ id: 'Central Brazilian Standard Time', iana: 'America/Cuiaba' },
{ id: 'Pacific SA Standard Time', iana: 'America/Santiago' },
{ id: 'Newfoundland Standard Time', iana: 'America/St_Johns' },
{ id: 'E. South America Standard Time', iana: 'America/Sao_Paulo' },
{ id: 'Argentina Standard Time', iana: 'America/Buenos_Aires' },
{ id: 'SA Eastern Standard Time', iana: 'America/Cayenne' },
{ id: 'Greenland Standard Time', iana: 'America/Godthab' },
{ id: 'Montevideo Standard Time', iana: 'America/Montevideo' },
{ id: 'Azores Standard Time', iana: 'Atlantic/Azores' },
{ id: 'Cape Verde Standard Time', iana: 'Atlantic/Cape_Verde' },
{ id: 'Morocco Standard Time', iana: 'Africa/Casablanca' },
{ id: 'GMT Standard Time', iana: 'Europe/London' },
{ id: 'Greenwich Standard Time', iana: 'Atlantic/Reykjavik' },
{ id: 'UTC', iana: 'UTC' },
{ id: 'W. Europe Standard Time', iana: 'Europe/Berlin' },
{ id: 'Central Europe Standard Time', iana: 'Europe/Budapest' },
{ id: 'Romance Standard Time', iana: 'Europe/Paris' },
{ id: 'Central European Standard Time', iana: 'Europe/Warsaw' },
{ id: 'W. Central Africa Standard Time', iana: 'Africa/Lagos' },
{ id: 'Jordan Standard Time', iana: 'Asia/Amman' },
{ id: 'GTB Standard Time', iana: 'Europe/Bucharest' },
{ id: 'Middle East Standard Time', iana: 'Asia/Beirut' },
{ id: 'Egypt Standard Time', iana: 'Africa/Cairo' },
{ id: 'South Africa Standard Time', iana: 'Africa/Johannesburg' },
{ id: 'FLE Standard Time', iana: 'Europe/Kiev' },
{ id: 'Israel Standard Time', iana: 'Asia/Jerusalem' },
{ id: 'E. Europe Standard Time', iana: 'Europe/Chisinau' },
{ id: 'Namibia Standard Time', iana: 'Africa/Windhoek' },
{ id: 'Arabic Standard Time', iana: 'Asia/Baghdad' },
{ id: 'Arab Standard Time', iana: 'Asia/Riyadh' },
{ id: 'Russian Standard Time', iana: 'Europe/Moscow' },
{ id: 'E. Africa Standard Time', iana: 'Africa/Nairobi' },
{ id: 'Georgian Standard Time', iana: 'Asia/Tbilisi' },
{ id: 'Iran Standard Time', iana: 'Asia/Tehran' },
{ id: 'Arabian Standard Time', iana: 'Asia/Dubai' },
{ id: 'Azerbaijan Standard Time', iana: 'Asia/Baku' },
{ id: 'Mauritius Standard Time', iana: 'Indian/Mauritius' },
{ id: 'Caucasus Standard Time', iana: 'Asia/Yerevan' },
{ id: 'Afghanistan Standard Time', iana: 'Asia/Kabul' },
{ id: 'Ekaterinburg Standard Time', iana: 'Asia/Yekaterinburg' },
{ id: 'Pakistan Standard Time', iana: 'Asia/Karachi' },
{ id: 'West Asia Standard Time', iana: 'Asia/Tashkent' },
{ id: 'India Standard Time', iana: 'Asia/Calcutta' },
{ id: 'Sri Lanka Standard Time', iana: 'Asia/Colombo' },
{ id: 'Nepal Standard Time', iana: 'Asia/Katmandu' },
{ id: 'N. Central Asia Standard Time', iana: 'Asia/Novosibirsk' },
{ id: 'Central Asia Standard Time', iana: 'Asia/Almaty' },
{ id: 'Myanmar Standard Time', iana: 'Asia/Rangoon' },
{ id: 'SE Asia Standard Time', iana: 'Asia/Bangkok' },
{ id: 'North Asia Standard Time', iana: 'Asia/Krasnoyarsk' },
{ id: 'China Standard Time', iana: 'Asia/Shanghai' },
{ id: 'North Asia East Standard Time', iana: 'Asia/Irkutsk' },
{ id: 'Singapore Standard Time', iana: 'Asia/Singapore' },
{ id: 'W. Australia Standard Time', iana: 'Australia/Perth' },
{ id: 'Taipei Standard Time', iana: 'Asia/Taipei' },
{ id: 'Tokyo Standard Time', iana: 'Asia/Tokyo' },
{ id: 'Korea Standard Time', iana: 'Asia/Seoul' },
{ id: 'Yakutsk Standard Time', iana: 'Asia/Yakutsk' },
{ id: 'Cen. Australia Standard Time', iana: 'Australia/Adelaide' },
{ id: 'AUS Central Standard Time', iana: 'Australia/Darwin' },
{ id: 'E. Australia Standard Time', iana: 'Australia/Brisbane' },
{ id: 'AUS Eastern Standard Time', iana: 'Australia/Sydney' },
{ id: 'West Pacific Standard Time', iana: 'Pacific/Port_Moresby' },
{ id: 'Tasmania Standard Time', iana: 'Australia/Hobart' },
{ id: 'Vladivostok Standard Time', iana: 'Asia/Vladivostok' },
{ id: 'Central Pacific Standard Time', iana: 'Pacific/Guadalcanal' },
{ id: 'New Zealand Standard Time', iana: 'Pacific/Auckland' },
{ id: 'Fiji Standard Time', iana: 'Pacific/Fiji' },
{ id: 'Tonga Standard Time', iana: 'Pacific/Tongatapu' }
]
const tz = timeZones.find(tz => tz.id === id);
return tz ? tz.iana: null;
}
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data-1970-2030.min.js"></script>