Javascript 在我当前的脚本中添加时区
Javascript adding Timezone in my current script
我的脚本中有这段代码来显示滴答作响的时钟
<script type="text/javascript">
function showTime() {
var date = new Date(),
time = new Date(Date.UTC(
date.getFullYear(),
date.getMonth(),
date.getDate(),
date.getHours(),
date.getMinutes(),
date.getSeconds()
));
document.getElementById('time').innerHTML = time.toLocaleTimeString();
}
setInterval(showTime, 1000);
</script>
任何人都知道如何将时区更改为特定时区,例如“Asia/Jakarta”
先谢谢了!
尝试这样的事情。
美国时间 = date.toLocaleString("en-US", {time Zone: "America/New_York"});
您可以将 undefined
更改为 'en-US'
=> 获取 country-specific 演示文稿
(en-US
是 AM/PM,fr-FR
-> 24 小时格式,undefined
-> 本地浏览器格式)
另见:
const
timeLocal = document.querySelector('#time-Local span')
, timeLA = document.querySelector('#time-US-LA span')
, timeJakarta = document.querySelector('#time-Jakarta span')
;
let currentDate = new Date();
timeLocal.textContent = currentDate.toLocaleTimeString()
timeLA.textContent = currentDate.toLocaleTimeString(undefined, {timeZone:'America/Los_Angeles'} )
timeJakarta.textContent = currentDate.toLocaleTimeString(undefined, {timeZone:'Asia/Jakarta'} )
p { width: 16em; }
p span { float: right;}
<p id="time-Local"> local time: <span></span> </p>
<p id="time-US-LA"> Los_Angeles time: <span></span> </p>
<p id="time-Jakarta"> Jakarta time: <span></span> </p>
我的脚本中有这段代码来显示滴答作响的时钟
<script type="text/javascript">
function showTime() {
var date = new Date(),
time = new Date(Date.UTC(
date.getFullYear(),
date.getMonth(),
date.getDate(),
date.getHours(),
date.getMinutes(),
date.getSeconds()
));
document.getElementById('time').innerHTML = time.toLocaleTimeString();
}
setInterval(showTime, 1000);
</script>
任何人都知道如何将时区更改为特定时区,例如“Asia/Jakarta” 先谢谢了!
尝试这样的事情。 美国时间 = date.toLocaleString("en-US", {time Zone: "America/New_York"});
您可以将 undefined
更改为 'en-US'
=> 获取 country-specific 演示文稿
(en-US
是 AM/PM,fr-FR
-> 24 小时格式,undefined
-> 本地浏览器格式)
另见:
const
timeLocal = document.querySelector('#time-Local span')
, timeLA = document.querySelector('#time-US-LA span')
, timeJakarta = document.querySelector('#time-Jakarta span')
;
let currentDate = new Date();
timeLocal.textContent = currentDate.toLocaleTimeString()
timeLA.textContent = currentDate.toLocaleTimeString(undefined, {timeZone:'America/Los_Angeles'} )
timeJakarta.textContent = currentDate.toLocaleTimeString(undefined, {timeZone:'Asia/Jakarta'} )
p { width: 16em; }
p span { float: right;}
<p id="time-Local"> local time: <span></span> </p>
<p id="time-US-LA"> Los_Angeles time: <span></span> </p>
<p id="time-Jakarta"> Jakarta time: <span></span> </p>