avatar

如何在html中动态显示当前时间
  1. 首先 编写如下的js文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    setInterval("fun(show_time)",10);
    function fun(timeID)

    var date = new Date();  //创建对象  
    var y = date.getFullYear();     //获取年份  
    var m =date.getMonth()+1;   //获取月份  返回0-11  
    var d = date.getDate(); // 获取日  
    var w = date.getDay();   //获取星期几  返回0-6   (0=星期天) 
    var ww = ' 星期'+'日一二三四五六'.charAt(new Date().getDay()) ;//星期几
    var h = date.getHours();  //时
    var minute = date.getMinutes()  //分
    var s = date.getSeconds(); //秒
    var sss = date.getMilliseconds() ; //毫秒
    if(m<10){m = "0"+m;}
    if(d<10){d = "0"+d;}
    if(h<10){h = "0"+h;}
    if(minute<10){minute = "0"+minute;}
    if(s<10){s = "0"+s;}
    if(sss<10){sss = "00"+sss;}else if(sss<100){sss = "0"+sss;}
    document.getElementById(timeID.id).innerHTML =y+"-"+m+"-"+d+" "+h+":"+minute+":"+s+ ww
    //document.getElementById(timeID.id).innerHTML =y+"-"+m+"-"+d+" "+h+":"+minute+":"+s+"."+sss+"  "+ww
    //document.write(y+"-"+m+"-"+d+"   "+h+":"+minute+":"+s);  
      }
  1. 在html中插入此文件

    1
    2
    3
    <div id="show_time">
    <script type="text/javascript" src="./JS/nowdate.js"></script>
    </div>
Author: Jie
Link: https://blog.jiehua1995.xyz/post/show_date_in_html/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • Wechat Pay
    Wechat Pay
  • Ali Pay
    Ali Pay

Comment