在网页上动态显示当前日期、星期、时间和周别的JS代码
  • 内容
  • 相关

在网页上动态显示当前日期、星期、时间和周别

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>在网页上动态显示当前日期、星期、时间和周别</title>
<script type="text/javascript">
    function theWeek() {
        var totalDays = 0;
        now = new Date();
        years = now.getYear()
        if (years < 1000) years += 1900
        var days = new Array(12);
        days[0] = 31;
        days[2] = 31;
        days[3] = 30;
        days[4] = 31;
        days[5] = 30;
        days[6] = 31;
        days[7] = 31;
        days[8] = 30;
        days[9] = 31;
        days[10] = 30;
        days[11] = 31;
     
        //判断是否为闰年,针对2月的天数进行计算
        if (Math.round(now.getYear() / 4) == now.getYear() / 4) {
            days[1] = 29
        } else {
            days[1] = 28
        }
 
        if (now.getMonth() == 0) {
            totalDays = totalDays + now.getDate();
        } else {
            var curMonth = now.getMonth();
            for (var count = 1; count <= curMonth; count++) {
                totalDays = totalDays + days[count - 1];
            }
            totalDays = totalDays + now.getDate();
        }
        //得到第几周
        var week = Math.round(totalDays / 7)+1;
        return week;
    }
</script>
</head>

<body>
    <div id="liveclock" style="color:#009900; font-size:14px;"></div>
</body>
<script type="text/javascript">
    function timeprint(){
	    var week; 
		var date;
		var today=new Date();
		var year=today.getFullYear();
		var month=today.getMonth()+1;
		var day=today.getDate();
		var ss=today.getDay();
		var hours=today.getHours();
		var minutes=today.getMinutes();
		var seconds=today.getSeconds();
		date=year+"年"+month+"月"+day+"日 ";
		if(ss==0) week="星期日";
		if(ss==1) week="星期一";
		if(ss==2) week="星期二";
		if(ss==3) week="星期三";
		if(ss==4) week="星期四";
		if(ss==5) week="星期五";
		if(ss==6) week="星期六";
		if(minutes<=9) minutes="0"+minutes;
		if(seconds<=9) seconds="0"+seconds;
		myclock=date+week+" "+hours+":"+minutes+":"+seconds+"  第"+theWeek()+"周";
		if(document.layers){
		    document.layers.liveclock.document.write(myclock);
			document.layers.liveclock.document.close();
		}else{
		    document.getElementById('liveclock').innerHTML=myclock;
		}
		setTimeout("timeprint()",1000)
    }
    timeprint();
</script>
</html>
点赞
X
赞助一下:
    支付宝    微信    QQ红包

打开支付宝扫一扫
在网页上动态显示当前日期、星期、时间和周别的JS代码
本文标签:
版权声明:若无特殊注明,本文皆为“懒人的小窝”原创,转载请保留文章出处。
本文链接:http://suppore.cn/274.html   百度已收录

发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论

1条评论
  1. avatar

    信阳毛尖 Lv.1    广东省广州市 电信    回复

    搞那么复杂有点眼晕,简洁点