I need to add a clock to a web page. The clock needs to be synchronized with a server but I really don\'t want to have it constantly check the server as the page will be ope
+Date.now()
returns local ms since the Unix epoch. So:
Or something like that.
Here's a fiddle demonstrating the first half:
var serverTime = 1310127993162; //this would come from the server obviously
var localTime = +Date.now();
var timeDiff = serverTime - localTime;
setInterval(function () {
console.log(+Date.now() + timeDiff);
}, 1000);
Server Time: <input type="text" id="clock" value="" size='8'>
<script type="text/javascript">
var serverTime = <?php echo time() * 1000; ?>; //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();
// minutes part from the timestamp
var minutes = date.getMinutes();
// seconds part from the timestamp
var seconds = date.getSeconds();
// will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes + ':' + seconds;
$('#clock').attr('value',formattedTime); <-- input id=clock
}, 1000);
</script>
The way I've gone about this before is:
You only need to update this occasionally from the server.
The problem that you've got to sort out though is that in making a request to get the time from the server, there will be a lag before you can get the client time. You can probably minimize this by using an ajax request to get the server time and nothing else, thereby cutting overhead and network lag etc.
one idea is to response datetime on page when it is requested. like:
<html>
your serveside codes,
<script>
var serverdatetime = new Date("<%=Datetime.Now%>");
//use serverdatetime and update it after 15 mins.
// you can use ajax to get datetime
setTimeout(function(){
$.ajax({
url: 'http://yourhost.com/getdate',
success: function( data ) {
// use data and update serverdatetime
}
});},54000);
</script>
server codes
</html>
**note : this is idea only, code may not work
Try below code this :
var m_serverDateTime;
var currentOffsetedTime;
var diffMilliseconds;
$(document).ready(function () {
m_serverDateTime = getServerDateTime();/*your function to get server time
by ajax call */
diffMilliseconds = GetServerTimeDifference(m_serverDateTime);
});
function GetServerTimeDifference(serverdatetime) {
var fromdate = new Date();
var todate = new Date(serverdatetime);
var localdiffMilliseconds = todate - fromdate;
return localdiffMilliseconds;
}
setInterval(function () {
//var currentOffsetedTime = new Date().setMinutes(diffMinutes);
currentOffsetedTime = new Date();
currentOffsetedTime.setMilliseconds(diffMilliseconds);
$('#lblTimer').text(format(currentOffsetedTime, 'dd/MM/yyyy HH:mm'));
}, 1000);
you just get one time date time from server, on load call this method and use moments js for format date:
var timeMiliSecond = new Date(serverTime).getTime();
callRepeatedly() {
setTimeout(() => {
timeMiliSecond = timeMiliSecond+1000;
serverTime=moment(newDate(timeMiliSecond)).format('MM-DD-YYYY HH:mm:ss');
this.callRepeatedly();
}, 1000)
}