2017-02-28 72 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

相关问题