把下面的代码贴到html文件里就可以了
function btnCount_Click(){
s1 = document.getElementById('sdate1').value;
s2 = document.getElementById('sdate2').value;
DateDiff(s1,s2);
}
//计算天数差的函数,通用
function DateDiff(sDate1, sDate2){ //sDate1和sDate2是2002-12-18格式
var aDate, oDate1, oDate2, iDays
aDate = sDate1.split("-")
oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为12-18-2002格式
aDate = sDate2.split("-")
oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24) //把相差的毫秒数转换为天数
month = Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24 /30
year = Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24 /365
alert('Day:'+iDays+' Month:'+month+' Years:'+ year +'/nage:' + Math.floor(year));
return iDays
}
第一种计算
第二种计算
下面是第二种计算方法:
请输入你的生日: 一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月 12345678910111213141516171819202122232425262728293031 |