Friday, 13 September 2013

Display UTC time incorrect after 12am

Display UTC time incorrect after 12am

I'm using a simple javascript to display the local time in my country on a
website, but it seems to be broken. After 12 midnight, by right the clock
should reset to AM instead, but it keeps showing the time in PM (eg. 1 PM
instead of 1 AM)
JS:
function updateClock (){
var currentTime = new Date();
var currentHours = currentTime.getUTCHours() + 8;
var currentMinutes = currentTime.getUTCMinutes();
var currentSeconds = currentTime.getUTCSeconds();
// var bucurestiOffset = 3*60000;
// var userOffset = currentTime.getTimezoneOffset()*60000;
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
currentHours = ( currentHours == 0 ) ? 12 : currentHours;
var currentTimeString = currentHours + ":" + currentMinutes + ":" +
currentSeconds + " " + timeOfDay;
document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
Don't know how to go about fixing this.

No comments:

Post a Comment