1 var clock = function(clockName){ 2 var mydate = new Date(); 3 var hours = mydate.getHours(); 4 var minutes = mydate.getMinutes(); 5 var seconds = mydate.getSeconds(); 6 var month = (mydate.getMonth()+1); 7 var date = mydate.getDate(); 8 var day = new Date().getDay(); 9 var text = ""; 10 switch (day) { 11 case 0: 12 text = "周日"; 13 break; 14 case 1: 15 text = "周一"; 16 break; 17 case 2: 18 text = "周二"; 19 break; 20 case 3: 21 text = "周三"; 22 break; 23 case 4: 24 text = "周四"; 25 break; 26 case 5: 27 text = "周五"; 28 break; 29 case 6: 30 text = "周六"; 31 break; 32 } 33 if(month < 10){ 34 month = '0' + month; 35 } 36 if(date < 10){ 37 date = '0' + date; 38 } 39 if(hours < 10){ 40 hours = '0' + hours; 41 } 42 if(minutes < 10){ 43 minutes = '0' + minutes; 44 } 45 if(seconds < 10){ 46 seconds = '0' + seconds; 47 } 48 var str = text +" "+ month + "-" + date + " " + hours +":"+ minutes; 49 $('#' + clockName).html("").text(str); 50 }
使用:
clock('clock');
setInterval(function(){
clock('clock');
},1000);