日期:2014-05-16 浏览次数:20464 次
<html> <head> <meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 "> <title> </title> </head> <body> <button onClick= "btnCount_Click() "> 计算相差天数 </button> <script language= "JavaScript "> function btnCount_Click(){ s1 = "2002-1-10 " s2 = "2002-10-1 " alert(DateDiff(s1,s2)) } //计算天数差的函数,通用 function DateDiff(sDate1, sDate2){ //sDate1和sDate2是2004-10-18格式 var aDate, oDate1, oDate2, iDays aDate = sDate1.split( "- ") oDate1 = new Date(aDate[1] + '- ' + aDate[2] + '- ' + aDate[0]) //转换为10-18-2004格式 aDate = sDate2.split( "- ") oDate2 = new Date(aDate[1] + '- ' + aDate[2] + '- ' + aDate[0]) iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24) //把相差的毫秒数转换为天数 return iDays } </script> </body> </html>