JS-Date日期内置对象
1、基本用法
<script> var date = new Date(); document.write(date+"<br/>");//获取具体时间 document.write(date.getFullYear()+"<br/>");//获取年份 document.write(date.getTime()+"<br/>");//获取1970年至今毫秒数 date.setFullYear(2010, 1, 1); document.write(date+"<br/>"); //自己设置时间 </script>
2、实现简单的时钟操作
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>uvi</title> 6 <link rel="stylesheet" href="style.css" type="text/css"> 7 </head> 8 <body onload="startTime()"> 9 <script> 10 /* 实现简单的时钟操作 */ 11 function startTime(){ 12 var today = new Date(); 13 var h = today.getHours(); 14 var m = today.getMinutes(); 15 var s = today.getSeconds(); 16 m = checkTime(m); 17 s = checkTime(s); 18 document.getElementById("timetxt").innerHTML = h+":"+m+":"+s; 19 t = setTimeout(function(){ 20 startTime(); 21 },500); 22 } 23 function checkTime(i){//如果是个位数,在前面补"0" 24 if(i<10){ 25 i="0"+i; 26 } 27 return i; 28 } 29 30 </script> 31 <div id = "timetxt"></div> 32 33 </body> 34 </html>