问题描述
- jQuery计算日期时间差和同步
-
如图片一样 计算俩个日期之间的时间差
并且更改开始时间或截止时间是下方的阴历开始跟截止同样改变,改变成同一天大神帮帮小弟 最好带注释 感谢了
<div class="maincontent"> <h2>在线日期计算</h2> <script>//显示当前日期时间 setInterval("jnkc.innerHTML=new Date().toLocaleString()+'星期'+'日一二三四五六'.charAt(new Date().getDay());",1000); </script> <div id="t1"> <p class="today">今天是</p><label id="jnkc" ></label> <div class="sj" ><span class="qz">起止</span><span class="jz">截止</span></div> <form name="reg_testdate" method="post" class="date"> <select name="YYYY" onChange="YYYYDD(this.value)"></select><span>年</span> <select name="MM" onChange="MMDD(this.value)"> </select><span>月</span> <select name="DD" ></select><span>日</span> </form> <form name="reg_testdate4" method="post" class="date4"> <select name="YYYY4" onChange="YYYYDD4(this.value)"></select><span>年</span> <select name="MM4" onChange="MMDD4(this.value)"> </select><span>月</span> <select name="DD4" ></select><span>日</span> </form> <form name="reg_testdate1" method="post" class="date1"> <select name="YYYY1" onChange="YYYYDD1(this.value)"></select><span>年</span> <select name="MM1" onChange="MMDD1(this.value)"> </select><span>月</span> <select name="DD1" ></select> </form> <form name="reg_testdate3" method="post" class="date3"> <select name="YYYY3" onChange="YYYYDD3(this.value)"></select><span>年</span> <select name="MM3" onChange="MMDD3(this.value)"> </select><span>月</span> <select name="DD3" ></select> </form> <form name="reg_testdate2" id="jg" class="date2"> <input type="text" class="tet" onChange="YYYYDD2(this.value)"/><span>年</span> <input type="text" class="tet" onChange="MMDD2(this.value)"/><span>月</span> <input type="text" class="tet"/><span>周</span> <input type="text" class="tet"/><span>日</span> <input type="text" class="tet"/><span>小时</span> <input type="text" class="tet"/><span>分钟</span> <input type="text" class="tet"/><span>秒</span> </form> </div> </div>
解决方案
var date1 = new Date(2012, 4, 7, 11, 27); //开始时间
var date2 = new Date(2012, 4, 7, 12, 28); //结束时间
var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数
//计算相差的年数
var years = Math.floor(date3 / (12 * 30 * 24 * 3600 * 1000));
//计算相差的月数
var leave = date3 % (12 * 30 * 24 * 3600 * 1000);
var months = Math.floor(leave / (30 * 24 * 3600 * 1000));
//计算出相差天数
var leave0 = leave % (30 * 24 * 3600 * 1000);
var days = Math.floor(leave0 / (24 * 3600 * 1000));
//计算出小时数
var leave1 = leave0 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
var hours = Math.floor(leave1 / (3600 * 1000));
//计算相差分钟数
var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
var minutes = Math.floor(leave2 / (60 * 1000));
//计算相差秒数
var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
var seconds = Math.round(leave3 / 1000);
document.write(" 相差 " + years + "年" + months + "月" + days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒")
解决方案二:
jquery 计算日期
jquery计算时间差
两个日期时间差
解决方案三:
var date1 = new Date(2012, 4, 7, 11, 27); //开始时间
var date2 = new Date(2012, 4, 7, 12, 28); //结束时间
var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数
//计算相差的年数
var years = Math.floor(date3 / (12 * 30 * 24 * 3600 * 1000));
//计算相差的月数
var leave = date3 % (12 * 30 * 24 * 3600 * 1000);
var months = Math.floor(leave / (30 * 24 * 3600 * 1000));
//计算出相差天数
var leave0 = leave % (30 * 24 * 3600 * 1000);
var days = Math.floor(leave0 / (24 * 3600 * 1000));
//计算出小时数
var leave1 = leave0 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
var hours = Math.floor(leave1 / (3600 * 1000));
//计算相差分钟数
var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
var minutes = Math.floor(leave2 / (60 * 1000));
//计算相差秒数
var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
var seconds = Math.round(leave3 / 1000);
document.write(" 相差 " + years + "年" + months + "月" + days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒")
解决方案四:
醉了,评论发多一条,也没法删掉,CSDN功能欠缺