2017-02-07 64 views
0

我有以下時區:如何使用nodejs獲取特定時區的當前小時?

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腳本。

回答

1

在不使用任何外部依賴性:

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)) 
 
})

+0

我喜歡你的答案, 爲什麼mod 24? 它是否適用於時區4.5,5.5和5.45? –

+0

@JuanPabloFernandez mod 24,所以答案總是在0到24之間。一天有24小時:-)是的,它可以用小數點時區。運行代碼片段並親自查看。 –

0

我建議你momentjs

var newYork = moment.tz("2014-06-01 12:00", "America/New_York"); 
var losAngeles = newYork.clone().tz("America/Los_Angeles"); 
var london  = newYork.clone().tz("Europe/London"); 

[https://momentjs.com/timezone/]

1

有一個偉大的圖書館稱爲moment.js特別時刻,timezone.js,這將有助於你的時區。

http://momentjs.com
http://momentjs.com/timezone/

所有你需要做的就是添加一切,你通過它需要到一個數組,然後循環的時區。

下面是一個例子:

var moment = require('moment-timezone'); 

var timezones = ['Africa/Abidjan', 'Africa/Accra']; 

timezones.forEach(function(timezone){ 
    var now = moment().utc(); 
    console.log(now.tz(timezone).toString()); 
}); 

編輯:
下面是所有可用的時區的鏈接:
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

0

您可以使用moment.js.First從當前時區取出時間並獲取小時數。

let date = moment().tz('Asia/Kolkata').format('LLLL') 
moment().get('hour'); 
相關問題