日期:2014-05-16  浏览次数:20437 次

js 计算两个日期中相差几年,几个月,几天
给定两个日期,计算这两个日期中相差了几年,几个月,几天

------解决方案--------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>获取时间差</title>
<style type="text/css">
*{margin:0;padding:0;}
#content{width:300px;margin:0 auto;padding:10px;background:#eee;border:1px solid #999;}
#content p span{color:red;font:bold 20px Arial;}
#content p a{font:12px/23px '宋体';color:#888;}
.div1{ display:none;}
</style>
<script type="text/javascript">
function fresh()
{
var endtime=new Date("2011/12/19,23:06:30");
var nowtime = new Date();
//两时间差为毫秒数,除以1000则转换为秒数
var leftsecond=parseInt((endtime.getTime()-nowtime.getTime())/1000);

d=parseInt(leftsecond/(3600*24));//计算出相差天数
h=parseInt((leftsecond/3600)%24);//扣除相差天数,计算出相差小时数
m=parseInt((leftsecond/60)%60);//扣除相差天数,小时数,计算出相差分钟数
s=parseInt(leftsecond%60);//扣除相差天数、小时数、分钟数相差数,计算出相差秒速

document.getElementById("times").innerHTML=d+'天'+h+"小时"+m+"分"+s+"秒";
setTimeout(fresh,1000);//每个一秒刷新一次数据

}
window.onload=fresh;
</script>
</head>
<body><div id="content">

<p>两者相差<span id="times"></span></p>
</div>

</body>
</html>