2017-02-28 67 views
-1

所以我正在製作一些數字時鐘,每個時鐘都有不同的時區,我已經完成了時鐘,現在我想讓它們顯示用戶時間區域設置。因此,如果它在美國顯示09:00 PM,而在歐洲則顯示21:00。用戶時間語言環境設置 - 使用javaScript(momentjs)?

這裏是時鐘

enter image description here

下面的圖片是代碼:

var elementsDigital = document.querySelectorAll("div[data-timezone]"); 
Array.prototype.forEach.call(elementsDigital, function(elem) { 
    var timezone = elem.getAttribute("data-timezone"); 
    elem.querySelector(".time").textContent = moment.tz(timezone).locale("en").format("LT"); 
}); 

基本上這個代碼只是得到的依據是什麼在HTML在你data-timezone,然後使用一些時區moment.js語言環境它將時區轉換爲英文,並顯示格式爲LT,這只是小時和分鐘。

所以,爲了讓您瞭解我,我希望根據用戶本地時間設置在我的時鐘中顯示不同的時區。所以美國和美國下午01:00 PM 13:00等。

+0

有什麼問題嗎?我沒有看到任何問題。 –

回答

0

找到答案:

var elementsDigital = document.querySelectorAll("div[data-timezone]"); 
Array.prototype.forEach.call(elementsDigital, function(elem) { 
    var timezone = elem.getAttribute("data-timezone"); 
    elem.querySelector(".time").textContent = moment.tz(timezone).locale(window.navigator.userLanguage || window.navigator.language).format("LT"); 
});` 

剛:window.navigator.userLanguage || window.navigator.language

相關問題