There are times when you might like to display server time on your web page, maybe even during development. This is the JavaScript that you can use to do just this.

Add the JavaScript to a file called serverTime.js.

var xmlHttp;
function srvTime(){
    try {
        //FF, Opera, Safari, Chrome
        xmlHttp = new XMLHttpRequest();
    catch (err1) {
        try {
            xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
        catch (err2) {
            try {
                xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
            catch (eerr3) {
                //AJAX not supported, use CPU time.
                alert("AJAX not supported");
    xmlHttp.setRequestHeader("Content-Type", "text/html");
    return xmlHttp.getResponseHeader("Date");

var st = srvTime();
var date = new Date(st);

Here is the HTML.

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Server date/time</title>
    <script language="javascript" src="serverTime.js"></script>
  <script language="javascript">
  var localTime = new Date();
  document.write("Local machine time is: " + localTime + "<br>");
  document.write("Server time is: " + date);


It took some time to find this information so hopefully, this helps you. Let us know if you have any questions or share how you are using this code in your project.

Article Tags:
Article Categories:

Web developer and former photographer by trade...gamer and all around tech enthusiast in his free time. Christoph started sdatic.com as a way to organize his ideas, research, notes and interests. He also found it enjoyable to connect and share with others who parallel his interests. Everything you see here is a resource that he found useful or interesting and he hopes that you will too.

Leave a Reply

Your email address will not be published. Required fields are marked *