JS实现——计算两日期之差

 

在网上找了个js实现的,根据相差天数计算日期和根据两个日期计算相差多少天的示例和代码:

根据相差天数计算日期:
距离:
相差: (输入负数则往前计算)
日期是:  



根据日期计算相差天数:
开始日期:
结束日期:
相差天数:  
相差年月日:  

 

 

=======================================================================================

实现代码如下:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>日期差计算器</title>
</head>

<body>
<script>
var hzWeek= new Array("","","","","","","","");

function cweekday(wday){return hzWeek[wday];}

function cala()
{
y=document.getElementById("SY").value;
m=document.getElementById("SM").value;
d=document.getElementById("SD").value;
ddd=document.getElementById("decday").value;

ttt=new Date(y,m-1,d).getTime()+ddd*24000*3600;

theday=new Date();
theday.setTime(ttt);

document.getElementById("result1").innerHTML=theday.getFullYear()+""+(1+theday.getMonth())+""+theday.getDate()+""+"星期"+cweekday(theday.getDay());
}

function calb()
{
y2=document.getElementById("SY2").value;
m2=document.getElementById("SM2").value;
d2=document.getElementById("SD2").value;


y3=document.getElementById("SY3").value;
m3=document.getElementById("SM3").value;
d3=document.getElementById("SD3").value;


day2=new Date(y2,m2-1,d2);
day3=new Date(y3,m3-1,d3);

document.getElementById("result2").innerHTML=(day3-day2)/86400000+"天";
}

function calc()
{
y2=parseInt(document.getElementById("SY2").value);
m2=parseInt(document.getElementById("SM2").value);
d2=parseInt(document.getElementById("SD2").value);

y1=parseInt(document.getElementById("SY3").value);
m1=parseInt(document.getElementById("SM3").value);
d1=parseInt(document.getElementById("SD3").value);


var day2ymrStr="";
        if (d2 > d1) {
            m1 = m1 - 1;
            d1 = d1 + 30;//这里每个月按30天计算,也可以根据月份计算天数
        }
        if (m2 > m1) {
            y1 = y1 - 1;
            m1 = m1 + 12;
        }
        d = (d1 - d2) + 1;
        m = m1 - m2;
        y = Math.abs(y1 - y2);
        if (y != 0) day2ymrStr += y + "";
        if (m != 0) day2ymrStr += m + "个月";
        if (d != 0) day2ymrStr += d + "";
        if (isNaN(d) || isNaN(m) || isNaN(y)) {
            return 0 + "";
        }
document.getElementById("result3").innerHTML=day2ymrStr;
        return day2ymrStr;

}

</script>

<b>根据相差天数计算日期:</b>
<br>
距离:
<input size=8 id=SY value=2015><input size=4 id=SM value=11><input size=4 id=SD value=8><b style="color:red;">(缺省为今天)</b>
<br>相差:
<input size=8 id=decday value=100><b style="color:red;">(输入负数则往前计算)</b>
<br />日期是:
<b><span id=result1>&nbsp;</span></b>
<br>
<input type=button value="计算" onclick="cala()">
<br><br><br>
<b>根据日期计算相差天数:</b>
<br>
开始日期:
<input size=8 id=SY2 value=2015><input size=4 id=SM2 value=11><input size=4 id=SD2 value=8><br>结束日期:
<input size=8 id=SY3  value=2016 ><input size=4 id=SM3 value=1><input size=4 id=SD3 value=1><br />相差天数:
<strong><span id="result2">&nbsp;</span></strong>
<br />
相差年月日:
<strong><span id="result3">&nbsp;</span></strong>
<br />
<input id="btnCalb" onclick="calWeek.calb()" type="button" value="计算相差天数" />
<input id="btnCalc" onclick="calWeek.calc()" type="button" value="计算相差年月日" />
<br />
<br>
</bodY>
</html>

 

 

 出处:http://bjtime.cn/riqi.asp

参考:https://www.cnblogs.com/lixingwu/p/7113590.html

posted on 2015-11-08 10:51  jack_Meng  阅读(11599)  评论(0编辑  收藏  举报

导航