js获取当前日期

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
 function curTime(){
 var now  = new Date();
 var year = now .getFullYear();
 var month =add_zero( now .getMonth()+1);
 var day =add_zero( now .getDate());
 var dy = now .getDay();//获取星期
 //星期格式化
 var week;
     switch (dy){
         case 1: week="星期一"; break;
         case 2: week="星期二"; break;
         case 3: week="星期三"; break;
         case 4: week="星期四"; break;
         case 5: week="星期五"; break;
         case 6: week="星期六"; break;
         default: week="星期天";
     }
 var hour = add_zero(now .getHours());
 var minute = add_zero(now .getMinutes());
 var second = add_zero(now .getSeconds());
 var time=year+"年"+month+"月"+day+"日"+"&nbsp;&nbsp;&nbsp;"+week+"&nbsp;&nbsp;&nbsp;"+hour+" :"+minute+" :"+second;
   document.getElementById("curTime").innerHTML=time;
  }
 //给不满10的数字前面加0
 function add_zero(temp)
 {
     if(temp<10) return "0"+temp;
     else return temp;
 }
//添加计时器,每一秒执行一次curTime()函数
 setInterval("curTime()",1000);
</script>
</head>

<body>
  <div id="curTime"></div>
</body>

</html>



显示效果:

2015年08月17日   星期一   11:38:45

你可能感兴趣的:(js)