node.js - 如何使用nodejs获取特定时区的当前小时?

标签 node.js

我有以下时区:

var timezones = [
  { value: '-12.0', str: '(GMT -12:00) Eniwetok, Kwajalein' },
  { value: '-11.0', str: '(GMT -11:00) Midway Island, Samoa' },
  { value: '-10.0', str: '(GMT -10:00) Hawaii' },
  { value: '-9.0', str: '(GMT -9:00) Alaska' },
  { value: '-8.0', str: '(GMT -8:00) Pacific Time (US & Canada)' },
  { value: '-7.0', str: '(GMT -7:00) Mountain Time (US & Canada)' },
  { value: '-6.0', str: '(GMT -6:00) Central Time (US & Canada), Mexico City' },
  { value: '-5.0', str: '(GMT -5:00) Eastern Time (US & Canada), Bogota, Lima' },
  { value: '-4.0', str: '(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz' },
  { value: '-3.5', str: '(GMT -3:30) Newfoundland' },
  { value: '-3.0', str: '(GMT -3:00) Brazil, Buenos Aires, Georgetown' },
  { value: '-2.0', str: '(GMT -2:00) Mid-Atlantic' },
  { value: '-1.0', str: '(GMT -1:00) Azores, Cape Verde Islands' },
  { value: '0.0', str: '(GMT) Western Europe Time, London, Lisbon, Casablanca' },
  { value: '+1.0', str: '(GMT +1:00) Brussels, Copenhagen, Madrid, Paris' },
  { value: '+2.0', str: '(GMT +2:00) Kaliningrad, South Africa' },
  { value: '+3.0', str: '(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg' },
  { value: '+3.5', str: '(GMT +3:30) Tehran' },
  { value: '+4.0', str: '(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi' },
  { value: '+4.5', str: '(GMT +4:30) Kabul' },
  { value: '+5.0', str: '(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent' },
  { value: '+5.5', str: '(GMT +5:30) Bombay, Calcutta, Madras, New Delhi' },
  { value: '+5.45', str: '(GMT +5:45) Kathmandu' },
  { value: '+6.0', str: '(GMT +6:00) Almaty, Dhaka, Colombo' },
  { value: '+7.0', str: '(GMT +7:00) Bangkok, Hanoi, Jakarta' },
  { value: '+8.0', str: '(GMT +8:00) Beijing, Perth, Singapore, Hong Kong' },
  { value: '+9.0', str: '(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk' },
  { value: '+9.5', str: '(GMT +9:30) Adelaide, Darwin' },
  { value: '+10.0', str: '(GMT +10:00) Eastern Australia, Guam, Vladivostok' },
  { value: '+11.0', str: '(GMT +11:00) Magadan, Solomon Islands, New Caledonia' },
  { value: '+12.0', str: '(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka' }
];

timezones.forEach(function(tz){
  console.log(tz.value);
  // Next how to calc current hour in the tz.value timezone ?
});

它们的值从 -12 到 12。 我需要循环时区并使用 nodejs 获取每个时区的当前小时。 这是一个 cronjob 的脚本。

最佳答案

不使用任何外部依赖项:

var timezones = [{
        value: '-12.0',
        str: '(GMT -12:00) Eniwetok, Kwajalein'
    },
    {
        value: '-11.0',
        str: '(GMT -11:00) Midway Island, Samoa'
    },
    {
        value: '-10.0',
        str: '(GMT -10:00) Hawaii'
    },
    {
        value: '-9.0',
        str: '(GMT -9:00) Alaska'
    },
    {
        value: '-8.0',
        str: '(GMT -8:00) Pacific Time (US & Canada)'
    },
    {
        value: '-7.0',
        str: '(GMT -7:00) Mountain Time (US & Canada)'
    },
    {
        value: '-6.0',
        str: '(GMT -6:00) Central Time (US & Canada), Mexico City'
    },
    {
        value: '-5.0',
        str: '(GMT -5:00) Eastern Time (US & Canada), Bogota, Lima'
    },
    {
        value: '-4.0',
        str: '(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz'
    },
    {
        value: '-3.5',
        str: '(GMT -3:30) Newfoundland'
    },
    {
        value: '-3.0',
        str: '(GMT -3:00) Brazil, Buenos Aires, Georgetown'
    },
    {
        value: '-2.0',
        str: '(GMT -2:00) Mid-Atlantic'
    },
    {
        value: '-1.0',
        str: '(GMT -1:00) Azores, Cape Verde Islands'
    },
    {
        value: '0.0',
        str: '(GMT) Western Europe Time, London, Lisbon, Casablanca'
    },
    {
        value: '+1.0',
        str: '(GMT +1:00) Brussels, Copenhagen, Madrid, Paris'
    },
    {
        value: '+2.0',
        str: '(GMT +2:00) Kaliningrad, South Africa'
    },
    {
        value: '+3.0',
        str: '(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg'
    },
    {
        value: '+3.5',
        str: '(GMT +3:30) Tehran'
    },
    {
        value: '+4.0',
        str: '(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi'
    },
    {
        value: '+4.5',
        str: '(GMT +4:30) Kabul'
    },
    {
        value: '+5.0',
        str: '(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent'
    },
    {
        value: '+5.5',
        str: '(GMT +5:30) Bombay, Calcutta, Madras, New Delhi'
    },
    {
        value: '+5.45',
        str: '(GMT +5:45) Kathmandu'
    },
    {
        value: '+6.0',
        str: '(GMT +6:00) Almaty, Dhaka, Colombo'
    },
    {
        value: '+7.0',
        str: '(GMT +7:00) Bangkok, Hanoi, Jakarta'
    },
    {
        value: '+8.0',
        str: '(GMT +8:00) Beijing, Perth, Singapore, Hong Kong'
    },
    {
        value: '+9.0',
        str: '(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk'
    },
    {
        value: '+9.5',
        str: '(GMT +9:30) Adelaide, Darwin'
    },
    {
        value: '+10.0',
        str: '(GMT +10:00) Eastern Australia, Guam, Vladivostok'
    },
    {
        value: '+11.0',
        str: '(GMT +11:00) Magadan, Solomon Islands, New Caledonia'
    },
    {
        value: '+12.0',
        str: '(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka'
    }
];

timezones.forEach(function(tz) {
    console.log(((new Date().getUTCHours() + parseFloat(tz.value)) % 24).toFixed(2))
})

关于node.js - 如何使用nodejs获取特定时区的当前小时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42097425/

相关文章:

node.js - 退出 Array.map() 循环

javascript - Jade 中的管符怎么转义?

javascript - 如何使用 React 构建 "Advanced Search Options"下拉菜单?

javascript - 将全局变量传递到函数的范围

node.js - Nginx 无法从 Node/Express 应用程序发送完整文件

node.js - 理解 Node.js 中的 try 和 catch

node.js - 继承 ORDER BY

node.js - 如何在 NodeJS 中比较来自请求正文的 HMAC SHA256 签名

node.js - socket.on 函数中未触发 Socket.get 回调

node.js - openssh 返回 "bad key type"以及加密生成的 key 对