1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <input type="text" id="input"/> 9 <script type="text/javascript"> 10 function counter() 11 { 12 var date = new Date(); //创建一个时间对象 13 var year = date.getFullYear();//先设置年 14 var date2 = new Date(year,12,31,23,59,59);//再设置目标时间 15 var time = (date2 - date)/1000; 16 var day = Math.floor(time/(24*60*60)); 17 var hour = Math.floor(time%(24*60*60)/(60*60)); 18 var min = Math.floor(time%(24*60*60)%(60*60)/60) 19 var ss = Math.floor(time%(24*60*60)%(60*60)%60) 20 var str = year +"年"+ day +"天"+hour +"时"+min +"分"+ss +"妙" 21 document.body.innerHTML= str; 22 console.log(day); 23 } 24 window.setInterval("counter()",1000);//调用定时器
// window.setInterval(counter,1000);//调用定时器 效果一样
25 </script> 26 </body> 27 </html>
原文:http://www.cnblogs.com/h5monkey/p/5855287.html