Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I need to get the current server time and display it on webpage .However it is displaying the machine time instead of server time.Could anyone help me please .Any help will be really appreciated.

What I have tried:

 <script type="text/javascript">

    function updateClock() {
        var currentTime = new Date();

        var currentHours = currentTime.getHours();
        var currentMinutes = currentTime.getMinutes();
        var currentSeconds = currentTime.getSeconds();

        // Pad the minutes and seconds with leading zeros, if required
        currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
        currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;

        // Choose either "AM" or "PM" as appropriate
        var timeOfDay = (currentHours < 12) ? "AM" : "PM";

        // Convert the hours component to 12-hour format if needed
        currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;

        // Convert an hours component of "0" to "12"
        currentHours = (currentHours == 0) ? 12 : currentHours;

        // Compose the string for display
        var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

        // Update the time display
        document.getElementById("clock").firstChild.nodeValue = currentTimeString;
        setTimeout(updateClock, 1000);
    }
    updateClock();

    
</script>
Posted
Updated 21-Feb-18 22:17pm
v2

JavaScript is a clientside scripting so, we can not get server time.
 
Share this answer
 
The question looked interesting and I tried to other opinion and I found following which actually works.

Create a js file e.g. serverDate.js with following content-
JavaScript
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);


HTML Source:
HTML
<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>


Credits: javascript - Right way to get Web Server time and display it on Web Pages - Stack Overflow[^]

Hope, it helps :)
 
Share this answer
 
Assuming webforms, if MVC use the same concept but in razor

<script type="text/javascript">
        var currentTime = new Date('<%=DateTime.Now.ToString("MMMM dd, yyyy, HH:mm:ss")%>');
        // rest of code
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900