Javascript 如何显示系统时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6787374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:10:52  来源:igfitidea点击:

How to display system time?

javascripthtml

提问by Bangaram

I want to display the system time on my jsp page. How can i do it? I'm trying this but only date is getting displayed that and not the time. It's all working fine in Internet Explorer but not in other browsers.

我想在我的jsp页面上显示系统时间。我该怎么做?我正在尝试这个,但只显示日期而不是时间。这在 Internet Explorer 中一切正常,但在其他浏览器中却没有。

  <td colspan="1" height="4" align="left" width="260" >
       <font class="welcome1">
            <strong>
                <script language="JavaScript" src="js/date.js"></script>
                <span id="clock">
                    <script language="JavaScript" 
                                            src="js/digitalClock.js"></script>
                </span>
            </strong>
       </font>
 </td>

回答by amal

Displaying the time on a web-page using js should be trivial .

使用 js 在网页上显示时间应该是微不足道的。

 new Date().toLocaleString() // displays date and time
 new Date().toLocaleDateString() // displays date
 new Date().toLocaleTimeString() // displays time

回答by kravemir

To display time you can use Date.

要显示时间,您可以使用Date

<html>
<head>
<script type="text/javascript">
    <!--
    function updateTime() {
        var currentTime = new Date();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        var seconds = currentTime.getSeconds();
        if (minutes < 10){
            minutes = "0" + minutes;
        }
        if (seconds < 10){
            seconds = "0" + seconds;
        }
        var v = hours + ":" + minutes + ":" + seconds + " ";
        if(hours > 11){
            v+="PM";
        } else {
            v+="AM"
        }
        setTimeout("updateTime()",1000);
        document.getElementById('time').innerHTML=v;
    }
    updateTime();
    //-->
</script>
</head>
<body>
    <h4>Current Time: <span id="time" /></h4>
</body>
</html>

I've tested itin firefox and chrome. Found on this site.

我已经在 Firefox 和 chrome 中测试过它。在这个网站上找到

Edit:time now gets updated every second.

编辑:时间现在每秒更新一次。

回答by Riskz

You should read the documentation about the Date object in JavaScript (Date). It would be easier if you post the JS source.

您应该阅读有关 JavaScript 中 Date 对象的文档 ( Date)。如果您发布JS源代码会更容易。

回答by Timo

This seems the easiest solution, which uses setInterval with two arguments, the first being a callback, the second the interval in ms:

这似乎是最简单的解决方案,它使用带有两个参数的 setInterval,第一个是callback,第二个是以毫秒为单位的间隔:

setInterval(function(){ document.write(new Date().toLocaleTimeString();

setInterval(function(){ document.write(new Date().toLocaleTimeString();

},1000);

},1000);

So my solution should be the accepted answer because it is a real time.

所以我的解决方案应该是公认的答案,因为它是实时的。

回答by Preeti Ranjan Sahoo

For Date You can use below Javascript. Time will display in a text box. You can modify as per your requirement.

对于日期,您可以使用以下 Javascript。时间将显示在文本框中。您可以根据您的要求进行修改。

<html>
<head>
<script type="text/javascript">
var timer = null
function stop()
{
clearTimeout(timer)
}
function start()
{
var time = new Date()
var hours = time.getHours()
var minutes = time.getMinutes()
var seconds = time.getSeconds()
var clock = hours + ":" + minutes + ":" + seconds
document.forms[0].display.value = clock
timer = setTimeout("start()",1000)
}
</script>
</head>
<body onload="start()" onunload="stop()">
<form>
<input type="text" name="display" size="20">
</form>
</body>
</html>

回答by antelove

/* set Date */
function tick() {

  /* Get date in epoch */
  var epoch = Date.now();    
  document.querySelector("#epoch").innerHTML = epoch;
  
  /* Separate epoch */
  var datetime = new Date(epoch);

  var year    = datetime.getFullYear();
  var month   = datetime.getMonth() + 1; // (0-11)
  var date    = datetime.getDate();
  var hour    = datetime.getHours();
  var minute  = datetime.getMinutes();
  var second  = datetime.getSeconds();

  document.querySelector("#datetime").innerHTML =
    year + "-" + addZero(month) + "-" + addZero(date) + " " + 
    addZero(hour) + ":" + addZero(minute) + ":" + addZero(second);      

}

// Add 0 if argument < 10
function addZero(i) {
  if (i < 10) {
    i = "0" + i
  };  
  return i;
}

/* Call tick in interval 1 second */
setInterval(tick, 1000);
<p id="epoch"></p>
<p id="datetime"></p>