我编写了一些自定义脚本以显示世界各地不同时区的时间时遇到问题。如果分钟数少于 10 分钟,它不会说 HH:MM,而是说 HH:M。我怎样才能改变它,如果它小于 10,它是 HH:0M?
截屏:
脚本:
$(document).ready(function() {
function calculateTime(offset) {
var currentDate = new Date()
utc = currentDate.getTime() + (currentDate.getTimezoneOffset() * 60000);
newDate = new Date(utc + (3600000*offset));
return (newDate.getHours() === 0) ? 12 : ((newDate.getHours() > 12) ? newDate.getHours() - 12 : newDate.getHours()) + ' : ' + newDate.getMinutes() + ' ' + ((newDate.getHours() > 11) ? 'PM' : 'AM');
}
function displayTimes() {
$(".chicago").html(calculateTime("-6"));
$(".london").html(calculateTime("+1"));
$(".shanghai").html(calculateTime("+8"));
};
displayTimes();
window.setInterval(displayTimes, 5000);
});
HTML:
<table>
<tr>
<td><strong>Chicago</strong></td>
<td align="right" class="time chicago">12:00PM</td>
</tr>
<tr>
<td><strong>London</strong></td>
<td align="right" class="time london">12:00PM</td>
</tr>
<tr class="last">
<td><strong>Shanghai</strong></td>
<td align="right" class="time shanghai">12:00PM</td>
</tr>
</table>