<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JS计算两个日期之间的天数</title>
</head>
<body>
<script type="text/javascript">
function btnCount_Click(){
s1 = "2014-12-30"
s2 = "2015-03-01"
alert("第一个日期;"+s1+"\n第二个日期:"+s2+"\n相差"+DateDiff(s1,s2)+"天")
}
//计算天数差的函数,通用
function DateDiff(sDate1, sDate2){ //sDate1和sDate2是2006-12-18格式
var aDate, oDate1, oDate2, iDays
aDate = sDate1.split("-")
oDate1 = new Date(aDate[1] + ‘-‘ + aDate[2] + ‘-‘ + aDate[0]) //转换为12-18-2006格式
aDate = sDate2.split("-")
oDate2 = new Date(aDate[1] + ‘-‘ + aDate[2] + ‘-‘ + aDate[0])
iDays = parseInt(Math.abs(oDate2 - oDate1) / 1000 / 60 / 60 /24) //把相差的毫秒数转换为天数
return iDays
}
</script>
<input name="" type="button" onclick="btnCount_Click()" value="计算天数" />
</body>
</html>