在 javascript 或 jquery 中显示实时时间、日期、星期几

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28689524/
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 02:15:01  来源:igfitidea点击:

Show Real Time, Date, Day of the Week in javascript or jquery

javascriptjquerydatetime

提问by kgam

How to show real time, date, day in this format?

如何以这种格式显示实时时间、日期、日期?

enter image description here

在此处输入图片说明

The time should be actual (which runs the seconds count).

时间应该是实际的(运行秒数)。

Thanks guys!

谢谢你们!

回答by Ivan Gerasimenko

To update time panel every second we should use setInterval()function.

要每秒更新时间面板,我们应该使用setInterval()函数。

To format date the way you need the best approach is to use moment.jslibrary. The code is shortened greatly:

要格式化日期,您需要的最佳方法是使用moment.js库。代码大大缩短:

$(document).ready(function() {
    var interval = setInterval(function() {
        var momentNow = moment();
        $('#date-part').html(momentNow.format('YYYY MMMM DD') + ' '
                            + momentNow.format('dddd')
                             .substring(0,3).toUpperCase());
        $('#time-part').html(momentNow.format('A hh:mm:ss'));
    }, 100);
});

Here is working fiddle

这是工作小提琴

回答by eLemEnt

You can use SetIntervalin javascript and run it after every 1 second. Look for the given example

您可以SetInterval在 javascript中使用并在每 1 秒后运行一次。寻找给定的例子

<!DOCTYPE html>
<html>
<body>

<p>A script on this page starts this clock:</p>
<p id="demo"></p>
<p id="demonew"></p>
<script>
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
  dd='0'+dd
} 

if(mm<10) {
  mm='0'+mm
} 

today = mm+'/'+dd+'/'+yyyy;
document.getElementById("demonew").innerHTML = today;
var myVar=setInterval(function(){myTimer()},1000);

function myTimer() {
    var d = new Date();
    document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
</script>

</body>
</html>

回答by YLG

You can use :
  <script>
    var mydate=new Date()
    var year=mydate.getYear()

if (year < 1000)
    year+=1900

var day=mydate.getDay() // Current Day of week - 2
var month=mydate.getMonth() // Current Month 2
var daym=mydate.getDate() // Current Date -24
var h=mydate.getHours(); //Hours
var m=mydate.getMinutes();//Minutes
var s=mydate.getSeconds();//Seconds
m = checkTime(m);
s = checkTime(s);

function checkTime(i) {
    if (i<10) {i = "0" + i};  // add zero in front of numbers < 10
return i;
}

Take 2 arrays

取2个数组

var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday",
                        "Friday","Saturday")
var montharray=new Array("January","February","March","April","May","June",
                        "July","August","September","October","November","December")

  document.getElementById('txt').innerHTML =h+":"+m+":"+s+" "+dayarray[day]+", "+montharray[month]+" "+daym+", "+year;

 var t = setTimeout(function(){startTime()},500);
  //This will update time 


 </script>
  <body onload="startTime()">

  <div id="txt"></div>

  </body>