Skip to content

Instantly share code, notes, and snippets.

@gouvermxt
Created August 25, 2011 18:59
Show Gist options
  • Save gouvermxt/1171489 to your computer and use it in GitHub Desktop.
Save gouvermxt/1171489 to your computer and use it in GitHub Desktop.
$(function($) {
init_clock();
setInterval(function() {
update_clock();
}, 1000);
//Inits the clock with date from server
function init_clock() {
// $('#datepicker').attr('data-current-time', '08:09:55');
$('#clock').text($('#datepicker').attr('data-current-time'));
}
function update_clock() {
var splited_time = $('#clock').text().split(':');
var current_second = parseInt(splited_time[2], 10) + 1;
var current_minute = parseInt(splited_time[1], 10);
var current_hour = parseInt(splited_time[0], 10);
if (current_second > 59) {
current_second = 0;
current_minute = current_minute + 1;
}
var new_hour = current_hour;
if (current_minute > 59) {
current_minute = 0;
if (current_hour == 12) new_hour = 13;
else if (current_hour == 23) new_hour = 0;
else new_hour = current_hour + 1;
}
var str_second = current_second.toString();
var str_minute = current_minute.toString();
var str_hour = new_hour.toString();
if (current_second <= 9) str_second = '0' + str_second;
if (current_minute <= 9) str_minute = '0' + str_minute;
if (new_hour <= 9) str_hour = '0' + str_hour;
var str_time = str_hour + ':' + str_minute + ':' + str_second;
$('#clock').text(str_time);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment