1 <!doctype html>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>无标题文档</title>
6 </head>
7
8 <body>
9 <p id="disBlock"></p>
10 </body>
11 <script type="text/javascript">
12 var results={millisecond:"", second:"",minute:"", hour:"",day:"",month:"",year:""}; //结果集
13 //计算剩余
14 function calcSurplus(fdate){
15 var odate = new Date(fdate); //未来的时间
16 var cdate, surplusDate; //当前时间,剩余时间
17 /* var year = new Date().getYear(); //当前年份
18 var february = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28; //计算二月份的天数
19 var days = new Array(31,february,31,30,31,30,31,31,90,31,30,31);*/
20 function result() {
21 cdate = new Date();
22 surplusDate = odate.getTime() - cdate.getTime(); //未来时间与当前时间相差的毫秒
23 with(results){
24 millisecond = surplusDate % 1000; //总毫秒数模1000就是换算为秒后剩余的毫秒
25 surplusDate -= millisecond; //总毫秒减去剩余的毫秒
26 second = (surplusDate / 1000) % 60; //(surplusDate / 1000)减去多余的毫秒剩下的毫秒/1000,
27 surplusDate -= second * 1000; //则为剩余毫秒转换为秒后的数值,然后用该数值模60则为剩下的分钟
28 minute = (surplusDate / 60000) % 60; //后面的运算以此类推,先减去前面已经算过的毫秒值
29 surplusDate -= minute * 60000; //然后用当前毫秒除以当前单位的运算值,然后取模该单位的上级单位
30 hour = (surplusDate / 3600000) % 24;
31 surplusDate -= hour * 3600000;
32 day = (surplusDate / 86400000) ;
33 /*surplusDate-=day*86400000;
34 month=(surplusDate / (86400000*days[cdate.getMonth()]))%12;
35 surplusDate-=month*(86400000*days[cdate.getMonth()]);
36 year=Math.floor(surplusDate/(86400000*(337+february))); //年没有上级单位,所以只需要用剩下的毫秒去除以换算值然后在向下取整就可以了*/
37 /* year=Math.floor(surplusDate/(86400000*(365))); //剩余年数
38 surplusDate-=year*31536000000;
39 for(var i=1;i<=year;i++){ //对年数循环,检查需要走过多少个闰年
40 if((cdate.getYear() % 4 == 0 && cdate.getYear() % 100 != 0) || cdate.getYear() % 400 == 0 ){
41 surplusDate-=86400000; //如果是闰年,多减一天的毫秒数
42 }
43 }*/
44 // 平年平均每月的毫秒数 2628000000
45 // 闰年平均每月的毫秒数 2635200000
46 /* var mis=february==28?2628000000:2635200000;
47 month=(surplusDate/mis)%12;
48 surplusDate-=month*mis;*/
49 }
50 return results;
51 }
52 return result;
53 }
54
55 var setHtml = calcSurplus("2015/07/12 16:08:11");
56
57 setInterval(function(){
58 setHtml();
59 var html=/*results.year+"年"+results.month+"月"+*/results.day+"天"+results.hour+"时"+results.minute+"分"+results.second+"秒"+results.millisecond+"毫秒";
60 document.getElementById("disBlock").innerHTML=html;
61 },1);
62 </script>
63 </html>