重写了一下,增强了通用性,重写时发现月份和年份计算很复杂,因为月份天数不一样,年份又涉及平年闰年;
本人数学实在不佳无法写出算法,哪位大大日后写出来了分享给小弟一份,谢谢~。


 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head> <body>
<p id="disBlock"></p>
</body>
<script type="text/javascript">
var results={millisecond:"", second:"",minute:"", hour:"",day:"",month:"",year:""}; //结果集
//计算剩余
function calcSurplus(fdate){
var odate = new Date(fdate); //未来的时间
var cdate, surplusDate; //当前时间,剩余时间
/* var year = new Date().getYear(); //当前年份
var february = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28; //计算二月份的天数
var days = new Array(31,february,31,30,31,30,31,31,90,31,30,31);*/
function result() {
cdate = new Date();
surplusDate = odate.getTime() - cdate.getTime(); //未来时间与当前时间相差的毫秒
with(results){
millisecond = surplusDate % ; //总毫秒数模1000就是换算为秒后剩余的毫秒
surplusDate -= millisecond; //总毫秒减去剩余的毫秒
second = (surplusDate / ) % ; //(surplusDate / 1000)减去多余的毫秒剩下的毫秒/1000,
surplusDate -= second * ; //则为剩余毫秒转换为秒后的数值,然后用该数值模60则为剩下的分钟
minute = (surplusDate / ) % ; //后面的运算以此类推,先减去前面已经算过的毫秒值
surplusDate -= minute * ; //然后用当前毫秒除以当前单位的运算值,然后取模该单位的上级单位
hour = (surplusDate / ) % ;
surplusDate -= hour * ;
day = (surplusDate / ) ;
/*surplusDate-=day*86400000;
month=(surplusDate / (86400000*days[cdate.getMonth()]))%12;
surplusDate-=month*(86400000*days[cdate.getMonth()]);
year=Math.floor(surplusDate/(86400000*(337+february))); //年没有上级单位,所以只需要用剩下的毫秒去除以换算值然后在向下取整就可以了*/
/* year=Math.floor(surplusDate/(86400000*(365))); //剩余年数
surplusDate-=year*31536000000;
for(var i=1;i<=year;i++){ //对年数循环,检查需要走过多少个闰年
if((cdate.getYear() % 4 == 0 && cdate.getYear() % 100 != 0) || cdate.getYear() % 400 == 0 ){
surplusDate-=86400000; //如果是闰年,多减一天的毫秒数
}
}*/
// 平年平均每月的毫秒数 2628000000
// 闰年平均每月的毫秒数 2635200000
/* var mis=february==28?2628000000:2635200000;
month=(surplusDate/mis)%12;
surplusDate-=month*mis;*/
}
return results;
}
return result;
} var setHtml = calcSurplus("2015/07/12 16:08:11"); setInterval(function(){
setHtml();
var html=/*results.year+"年"+results.month+"月"+*/results.day+"天"+results.hour+"时"+results.minute+"分"+results.second+"秒"+results.millisecond+"毫秒";
document.getElementById("disBlock").innerHTML=html;
},);
</script>
</html>


JS倒计时功能,给定时间返回规定格式倒计时时间的更多相关文章

  1. c# 处理js序列化时 datetime返回UTC格式的问题

    using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Syst ...

  2. 点滴积累【JS】---JS小功能(setInterval实现图片效果显示时间)

    效果: 代码: <head runat="server"> <title></title> <script type="text ...

  3. JS实现为控件添加倒计时功能

    一.概述 在有些报表需求中,需要为控件添加倒计时功能,限制到某一个时间点后能进行一项操作或不能进行某项操作,比如查询,导出功能等等,又需要人性化地显示还有多少时间,即倒计时功能,比如下图中我们限制这个 ...

  4. 原生js实现一个简单的倒计时功能

    大家好,我是云中君!欢迎大家来观看我的博客 之前那,在群里看到很多人问,关于电商网站中的倒计时功能怎么实现,很多人说在网上找了很多插件,但是不是很会用,所以今天就在这里分享一下我封装的一个小的倒计时功 ...

  5. js之验证码倒计时功能

    <!DOCTYPE html> <html > <head> <meta http-equiv="Content-Type" conten ...

  6. 模块:js实现一个倒计时功能

    1.给显示内容加样式 <style> #p1{font-size: large; color: red;} </style> 2.客户端页面 <div id=" ...

  7. js实现是倒计时功能

    工作中经常用到倒计时的功能,最近在整理之前做的项目的时候,发现自己写过一个倒计时的功能的效果,这里和大家分享下!实现这个功能是用原生js写的,不需要加载额外的库文件!功能比较简单,但是可以在此基础上扩 ...

  8. 微信小程序注册60s倒计时功能 使用JS实现注册60s倒计时功能

    微信小程序+WEB使用JS实现注册[60s]倒计时功能开发步骤: 1.wxml页面代码: <text>绑定手机</text> <form bindsubmit=" ...

  9. js 倒计时功能,获取当前时间的年月日,时分秒

    一.实现当前时间到指定截止时间的倒计时功能 <html> <head> <title>TEST</title> </head> <bo ...

随机推荐

  1. Canvas 实现七彩喷泉

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. Unity中使用WebView

    Unity中使用WebView @(设计) 需求,最近游戏中需要引入H5直播页面和更新比较频繁的赛事页面,需求包括:加密传参数.和Unity交互,在Unity框架下其实有几种方案: 内置函数Appli ...

  3. linux添加环境变量(centos)

    1.查看当前环境变量 #echo $PATH 2.增加环境变量 #vi /etc/profile export PATH=/usr/path/bin:$PATH 3.生效 #source /etc/p ...

  4. VECTOR COMPUTATION

    COMPUTER OR GANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Although the perform ...

  5. Mysql 获取年级每个班前十学生的信息

    select * from Table1 a where 10>(select count(*) from Table1 where ClsNo=a.ClsNo and Score>a.S ...

  6. QHash

    #include <QCoreApplication> #include<QHash> #include<QDebug> int main(int argc, ch ...

  7. Spring操作指南-IoC基础环境配置(基于XML)

  8. 记在virtualbox下挂载共享文件夹的方法

    sudo mount -t vboxsf share /usr/share sudo mount -t vboxsf 共享文件夹名称(在设置页面设置的) 挂载的目录

  9. 当利用pip安装模块出现错误时咋办

    >在DOS窗口中到Python安装路径的scripts中执行  pip install pyperclip 出现错误 >>错误提示:Fatal error in launcher: ...

  10. Android OnTouchListener 导致 OnClickListener 接收不到消息 无反应

    原因是onTouch返回了true把消息给消耗掉了,所以OnClickListener接收不到消息. 解决办法:onTouch返回false