JS 计算两个日期相差天数
<script language="JavaScript">
function ComputeInterval()
{
//alert(new Date());//当前时间
//alert((new Date(1970,1,1)).getTime());
var strFirstDate = document.all.txtFirstDate.value;
var strSecondDate = document.all.txtSecondDate.value;
//====================================================================================
var FirstDate_Year = parseInt( strFirstDate.substring(0,strFirstDate.indexOf("年")) );
//alert(FirstDate_Year);//开始年
var FirstDate_Month = parseInt( strFirstDate.substring(strFirstDate.indexOf("年")+1,strFirstDate.indexOf("月")) );
//alert(FirstDate_Month);//开始月
var FirstDate_Day = parseInt( strFirstDate.substring(strFirstDate.indexOf("月")+1,strFirstDate.indexOf("日")) );
//alert(FirstDate_Day);//开始日
var FirstDate = new Date(FirstDate_Year,FirstDate_Month,FirstDate_Day);
//alert(FirstDate.getTime());
var FirstDate_Time = FirstDate.getTime();
//====================================================================================
var SecondDate_Year = parseInt( strSecondDate.substring(0,strSecondDate.indexOf("年")) );
//alert(SecondDate_Year);//结束年
var SecondDate_Month = parseInt( strSecondDate.substring(strSecondDate.indexOf("年")+1,strSecondDate.indexOf("月")) );
//alert(SecondDate_Month);//结束月
var SecondDate_Day = parseInt( strSecondDate.substring(strSecondDate.indexOf("月")+1,strSecondDate.indexOf("日")) );
//alert(SecondDate_Day);//结束日
var SecondDate = new Date(SecondDate_Year,SecondDate_Month,SecondDate_Day);
//alert(SecondDate.getTime());
var SecondDate_Time = SecondDate.getTime();
//====================================================================================
var IntervalTime = SecondDate_Time-FirstDate_Time;//两个日期相差的毫秒数 一天86400000毫秒
var Inter_Days = (IntervalTime).toFixed(2)/86400000;
document.all.txtInterval.value= Inter_Days.toFixed(2) + "天";
}
</script>
<input type="text" value="2006年12月13日" id="txtFirstDate" />
<input type="text" value="2006年12月14日" id="txtSecondDate" />
<input type="button" value="计算相差天数" onclick="ComputeInterval()" />
<input type="text" id="txtInterval">