<!-- 重点:学习Date对象的方法和js基础逻辑. -->
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>时间</title> 6 <script type="text/javascript"> 7 function curTime() 8 { 9 var time = new Date(); 10 var year = time.getFullYear(); 11 var mouth = time.getMonth(); 12 var day = time.getDate(); 13 var hour = time.getHours(); 14 var minute = time.getMinutes(); 15 var second = time.getSeconds(); 16 var week = time.getDay(); 17 //将系统星期转换为中文星期 18 //第一种,借用数组方法 19 var weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]; 20 //使用if+else判断 21 var day1; 22 if(week == 0){ 23 day1 = "星期日"; 24 }else if(week == 1){ 25 day1 = "星期一"; 26 }else if(week == 2){ 27 day1 = "星期二"; 28 }else if(week == 3){ 29 day1 = "星期三"; 30 }else if(week == 4){ 31 day1 = "星期四"; 32 }else if(week == 5){ 33 day1 = "星期五"; 34 }else{ 35 day1 = "星期六"; 36 } 37 38 //使用switch+case判断 39 var day2; 40 switch(week){ 41 case 0: 42 day2 = "星期日"; 43 break; 44 case 1: 45 day2 = "星期一"; 46 break; 47 case 2: 48 day2 = "星期二"; 49 break; 50 case 3: 51 day2 = "星期三"; 52 break; 53 case 4: 54 day2 = "星期四"; 55 break; 56 case 5: 57 day2 = "星期五"; 58 break; 59 default: 60 day2 = "星期六"; 61 } 62 63 //使用charat()函数 64 var day3 = "星期" + "日一二三四五六".charAt(time.getDay()); 65 66 var cur =year + "/" + mouth + "/" + day + " " + hour + ":" + minute + ":" + second + " " + day3; 67 68 var times = document.getElementById("times"); 69 times.innerHTML = cur; 70 } 71 72 setInterval(curTime,1000); 73 </script> 74 </head> 75 <body> 76 <div id=times> 77 </div> 78 </body> 79 </html>