TimeAPI-based JavaScript clock

I want to create a JavaScript watch based on TimeAPI.org . The reason is because I want to make sure that the client side has the same time as the server.

In svclock(json)it saves year / month / day / hour / ... etc. in vars, but when I try to put these vars in setHoursmy fucnction watch, the clock format will look like this:
1391670641381:1391670761381:1391670727381  instead 15:27:06.

Is there an easier way to do this?

<html>
<head>


<script>
var svtime,svyear,svmonth,svdate,svday,svhour,svmin,svsec,newtime;

function svclock(json) {
    svtime=new Date(json.dateString);
    svyear=String(svtime).substr(11,4);
    svmonth=String(svtime).substr(4,3);
    svdate=String(svtime).substr(8,2);
    svday=String(svtime).substr(0,3);
    svhour=String(svtime).substr(16,2);
    svmin=String(svtime).substr(19,2);
    svsec=String(svtime).substr(22,2);
    newtime=svhour+':'+svmin+':'+svsec;
    //newtime=svyear+'/'+svmonth+'/'+svdate+'('+svday+')'+' '+svhour+':'+svmin+':'+svsec;
}

function startTime()
{
    var today=new Date();
    var h=today.setHours(svhour);
    var m=today.setMinutes(svmin);
    var s=today.setSeconds(svsec);

    document.getElementById('clock').innerHTML=h+":"+m+":"+s;
    t=setTimeout(function(){startTime()},1000);
}
</script>


<script type="text/javascript" src="http://timeapi.org/gmt/now.json?callback=svclock"></script>


</head>

<body onload="startTime()">
<div id="clock"></div>
</body>
</html>
+3
source share

All Articles