Skip to content

Instantly share code, notes, and snippets.

@hellais
Created April 9, 2012 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellais/4633bfa12c5095588e96 to your computer and use it in GitHub Desktop.
Save hellais/4633bfa12c5095588e96 to your computer and use it in GitHub Desktop.
Date Checker with HTTP headers
<html>
<head>
<title>Date checker</title>
</head>
<body>
<script type="text/javascript">
// Set this value to the minute tollerance
var minute_limit = 30;
function get_date_header(address) {
// Address must not violate SOP, if it
// does then browsers will not allow to read
// the "Date" header.
var client = new XMLHttpRequest();
client.open('GET', address, false);
client.send(null);
console.log(client);
var hdate = client.getResponseHeader("Date");
return hdate;
}
date_header = get_date_header("/date.html");
var server_date = new Date(date_header);
var now = new Date();
var m_offset = (now - server_date)/(60*1000);
if (m_offset > minute_limit) {
document.write("Your clock is off by "+m_offset+" minutes");
} else {
document.write("Your clock is synchronized to at least "+minute_limit+" minutes");
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment