在JavaScript中获取当前服务器时间 [英] Get current server time in javascript

查看:673
本文介绍了在JavaScript中获取当前服务器时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要获取当前服务器时间并将其显示在网页上.但是它显示的是机器时间而不是服务器时间.任何人都可以帮助我.任何帮助将不胜感激.

我尝试过的事情:

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>

推荐答案

JavaScript是一种客户端脚本,因此,我们无法获取服务器时间.
JavaScript is a clientside scripting so, we can not get server time.


这个问题看起来很有趣,我尝试了其他意见,但发现以下方法确实有效.

创建一个js文件,例如具有以下内容的serverDate.js-
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-
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来源:



HTML Source:

<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>



鸣谢: javascript-获取Web服务器时间并将其显示在网页上的正确方法-堆栈溢出 [



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

Hope, it helps :)


假设Web表单,如果MVC使用相同的概念但使用剃刀,则为

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>


这篇关于在JavaScript中获取当前服务器时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆