巴西世界杯是足球迷的今年最终关注的事情,做为球迷的我也不例外,自己做了一个巴西世界杯的倒计时。
<!DOCTYPE html> <html> <head> <title>巴西世界杯倒计时时间</title> <meta charset="utf-8" /> </head> <body> <h2>巴西世界杯倒计时:</h2> <p id="time">时间加载中......</p> <script type="text/javascript"> //2014-04-20 12:10:18 var to1 = 2014; var to2 = 6; var to3 = 13; var to4 = 4; var to5 = 0; var to6 = 0; var time = document.getElementById(‘time‘); function timer(){ var now = new Date(); var y = now.getFullYear(); var m = now.getMonth(); var d = now.getDate(); var h = now.getHours(); var me = now.getMinutes(); var s = now.getSeconds(); var s1 = to1-y; var s2 = to2-m-1; var s3 = to3-d; var s4 = to4-h; var s5 = to5-me; var s6 = to6-s; if(s2<0){ s2 = s2+12; s1 = s1-1; } if(s3<0){ if(m==2){ if((y%4==0 && y%100!=0) || y%400 ==0){ s3 = s3+29; }else{ s3 = s3+28; } }else if(m==4|| m==6|| m==8 || m==11){ s3 = s3+30; }else{ s3 = s3+31; } s2 = s2-1; } if(s4<0){ s4 = s4+24; s3 = s3-1; } if(s5<0){ s5 = s5+60; s4 = s4-1; } if(s6<0){ s6 = s6+60; s5 = s5-1; } time.innerHTML = "现在距离"+to1+"年"+to2+"月"+to3+"日"+to4+":"+to5+":"+to6+"还有:"+s1+"年"+s2+"个月"+s3+"天"+s4+"小时"+s5+"分"+s6+"秒"; } setInterval(function(){timer()},1000); </script> </body> </html>
运行效果如下: