3 回答
TA贡献2037条经验 获得超6个赞
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
function startTime() {
var asiaDhaka = new Date().toLocaleString([], { timeZone: "Asia/Dhaka" });
var today = new Date(asiaDhaka);
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
// add a zero in front of numbers<10
var isFormat12H = true;
var ampm = "";
if (isFormat12H) {
ampm = h >= 12 ? "pm" : "am";
h = h % 12;
h = h ? h : 12;
}
m = checkTime(m);
s = checkTime(s);
document.getElementById("time").innerHTML = h + ":" + m + ":" + s + ampm;
t = setTimeout(function () {
startTime();
}, 1000);
}
startTime();
TA贡献1890条经验 获得超9个赞
使用Intl.DateTimeFormat API
let options = {
timeZone: 'Asia/Dhaka',
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
},
myDate = new Intl.DateTimeFormat([], options);
setInterval(() => {
console.log(myDate.format(new Date()));
}, 1000);
您可能需要检查浏览器支持
TA贡献1860条经验 获得超8个赞
用这个:
let time = new Date().toLocaleString("en-US", { timeZone: "Asia/Dhaka" });
添加回答
举报