3 回答

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊
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貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊
使用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貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊
用這個(gè):
let time = new Date().toLocaleString("en-US", { timeZone: "Asia/Dhaka" });
添加回答
舉報(bào)