我找到了一个脚本,可以显示不同时区的当前日期和时间。但是我无法更改其格式。目前它显示为
MM/DD/YYYY HH:MM:SS AM/PM
,而我想要它只显示为HH:MM AM/PM
我对jQuery比纯JavaScript更熟练,但这让我困扰:$(document).ready(function() {
function calcTime(offset) {
currentDate = new Date();
utc = currentDate.getTime() + (currentDate.getTimezoneOffset() * 60000);
newDate = new Date(utc + (3600000*offset));
return newDate.toLocaleString();
}
function displayTimes() {
$("#chicago").html(calcTime("-6"));
$("#london").html(calcTime("+1"));
$("#shanghai").html(calcTime("+8"));
};
window.setInterval(displayTimes, 1000);
});
newDate.toLocaleString()
函数,并在其中创建一个字符串,从 http://www.w3schools.com/jsref/jsref_obj_date.asp 中获取相关部分。 - Sudipta Chatterjee