function start_navigation_clock_holder_counter() { var serverTime = 1762581871000; //this would come from the server var localTime = +Date.now(); var timeDiff = serverTime - localTime; setInterval(function () { var realtime = +Date.now() + timeDiff; var date = new Date(realtime); // hours part from the timestamp var hours = date.getHours(); var hours_f = (hours > 12) ? (hours-12) : (hours == 0) ? 12 : (hours); // minutes part from the timestamp var minutes = date.getMinutes(); var minutes_str = minutes.toString(); if (minutes_str.length < 2) {minutes = '0' + minutes;} // seconds part from the timestamp var seconds = date.getSeconds(); var seconds_str = seconds.toString(); if (seconds_str.length < 2) {seconds = '0' + seconds;} var am_pm = (hours >= 12) ? 'PM' : 'AM'; // will display time in 10:30:23 format var formattedTime = hours_f + ':' + minutes + ':' + seconds + ' ' + am_pm; //$('#navigation_clock_holder').attr('value',formattedTime); write_html('navigation_clock_holder', formattedTime); }, 1000); }