I'm having an issue with a bit of custom script that I wrote to show the time in different time zones around the world. Instead of saying HH:MM, if the minutes are less than 10, it says HH:M. How can I change it so that if it's less then 10, it sas HH:0M?
Screenshot:
Script:
$(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>