Skip to content

Instantly share code, notes, and snippets.

@bububa
Created May 18, 2012 09:09
Show Gist options
  • Save bububa/2724159 to your computer and use it in GitHub Desktop.
Save bububa/2724159 to your computer and use it in GitHub Desktop.
javascript 实时获得服务器上时间
var http = new XMLHttpRequest;
http.open("HEAD", ".", false);
http.send(null);
var curDate = new Date;
var offsetTime = curDate - Date.parse(http.getResponseHeader("Date"));
setInterval(function()
{
curDate.setTime(new Date - offsetTime);
document.getElementById("spnTime").innerHTML = curDate.toLocaleString();
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment