<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<button onClick="btnDayCount_Click();">计算相差天数</button>
<script type="text/javascript" > // language="JavaScript"
function btnDayCount_Click(){
// alert("计算相差天数");
var day1 = "2002-1-10" ;
var day2 = "2002-10-10" ;
alert(DateDiff(day1,day2)) ;
}
//计算天数差的函数,通用
function DateDiff(sDate1, sDate2){ //sDate1和sDate2是2002-12-18格式
var aDate, oDate1, oDate2, iDays ;
aDate = sDate1.split("-") ;
/* for (var i = 0; i < aDate.length; i++) {
alert(aDate[i]);
} */
oDate1 = new Date(aDate[1] + ‘-‘ + aDate[2] + ‘-‘ + aDate[0]) ; //转换为12-18-2002格式
aDate = sDate2.split("-") ;
oDate2 = new Date(aDate[1] + ‘-‘ + aDate[2] + ‘-‘ + aDate[0]) ;
//Math.abs(num)求出绝对值
//parseInt(num)解析字符串,返回一个整数
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24) ; //把相差的毫秒数转换为天数
return iDays;
}
</script>
</body>
</html>