www.webdeveloper.com
Results 1 to 3 of 3

Thread: Getting server date/time with no server side script

  1. #1
    Join Date
    Apr 2010
    Posts
    1

    Lightbulb Getting server date/time with no server side script

    Hi!

    I'd been looking for some way to get server date/time without using any server side script (such as ASP, PHP, etc).

    I found this and it worked just excellent for me! I just want to share it because it wasn't easy to find.

    I created a js document with code:

    Code:
    var xmlHttp;
    function srvTime(){
    	try {
    		//FF, Opera, Safari, Chrome
    		xmlHttp = new XMLHttpRequest();
    	}
    	catch (err1) {
    		//IE
    		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.open('HEAD',window.location.href.toString(),false);
    	xmlHttp.setRequestHeader("Content-Type", "text/html");
    	xmlHttp.send('');
    	return xmlHttp.getResponseHeader("Date");
    }
    
    var st = srvTime();
    var date = new Date(st);
    Now, I can use this js within a html like this:

    HTML Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Server date/time</title>
    <script language="javascript" src="serverDate.js"></script>
    </head>
    <script language="javascript">
    var localTime = new Date();
    document.write("Local machine time is: " + localTime + "<br>");
    document.write("Server time is: " + date);
    </script>
    <body>
    </body>
    </html>
    You may change your machine date/time and check both dates.

  2. #2
    Join Date
    Feb 2006
    Posts
    2,927
    (Moderator, Please delete this post)
    Last edited by mrhoo; 04-19-2010 at 09:43 PM.

  3. #3
    Join Date
    Jun 2012
    Posts
    1

    Server Time Delta

    Thanks! I used your posting for my solution:

    http://stackoverflow.com/questions/5...01162#11001162

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles