JS获取当前时间

1.
<script language="JavaScript">
//日期 
var now = new Date(); //获取系统日期,即Sat Jul 29 08:24:48 UTC+0800 2006 
var yy = now.getYear(); //截取年,即2006 
var mm = now.getMonth(); //截取月,即07 
var dd = now.getDay(); //截取日,即29 
//取时间 
var hh = now.getHours(); //截取小时,即8 
var mm = now.getMinutes(); //截取分钟,即34 
var ss = now.getTime() % 60000; //获取时间,因为系统中时间是以毫秒计算的, 
所以秒要通过余60000得到。 
ss = (ss - (ss % 1000)) / 1000; //然后,将得到的毫秒数再处理成秒 
var clock = hh+':'; //将得到的各个部分连接成一个日期时间 
if (mm < 10) clock += '0'; //字符串 
clock += mm+':';  
if (ss < 10) clock += '0';  
clock += ss; 
</script>


2.

</head> 
<body>

<script language="javascript">
var week; 
if(new Date().getDay()==0)          week="周日"
if(new Date().getDay()==1)          week="周一"
if(new Date().getDay()==2)          week="周二" 
if(new Date().getDay()==3)          week="周三"
if(new Date().getDay()==4)          week="周四"
if(new Date().getDay()==5)          week="周五"
if(new Date().getDay()==6)          week="周六"
document.write((new Date().getMonth()+1)+"月"+new Date().getDate()+"日 "+week);
</script>

</body>
</html>

3.

<!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" xml:lang="en" lang="en">
<head>
<title></title>
<script type="text/javascript">
   function clock(){
       var nowtime=new Date();
       var flag="AM";
       var hours=nowtime.getHours();
       if(hours>12){
           hours-=12;
           flag="PM";
       }
       hours=hours>9?hours:"0"+hours;
       var minutes=nowtime.getMinutes();
       minutes=minutes>9?minutes:"0"+minutes;
       var seconds=nowtime.getSeconds();
       seconds=seconds>9?seconds:"0"+seconds;
       var disptime=hours+":"+minutes+":"+seconds+" "+flag;
       document.getElementById("clock").innerHTML=disptime;
       setTimeout("clock()",1000);
   }
</script>
</head>
<body>
<div id="clock"></div>
<script type="text/javascript">
clock();
</script>
</body>
</html>

  

posted @ 2012-02-06 16:50  cyjch  阅读(16169)  评论(0编辑  收藏  举报