JavaScript clock based on TimeAPI

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

In svclock(json) it saves year/month/day/hour/…etc in vars
but when I’m trying to put those vars in setHours in my clock fucnction
the clock’s format becomes like this:
1391670641381:1391670761381:1391670727381
instead of 15:27:06.

  • Javascript show milliseconds as days:hours:mins without seconds
  • Convert times created by Javascript's getTime() method to ruby Time objects
  • Detecting changes to system time in JavaScript
  • JavaScript won't parse GMT Date/Time Format
  • Convert Microsoft JSON Date to Local DateTime Using Moment JS Only
  • new Date() set to 31 december 2014 says 1st december instead
  • 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>
    

  • How to convert date in RFC 3339 to the javascript date object(milliseconds since 1970)
  • What is the Best Practice for Encoding Dates in JSON?
  • Convert time interval given in seconds into more human readable form
  • Convert PHP date into javascript date format
  • Javascript, Time and Date: Getting the current minute, hour, day, week, month, year of a given millisecond time
  • Calculating year, month, days between dates in google apps script