js 日期相差的天数
/*js将yyyymmddhhmmss字符date计算相差天数*/ function getDifDays( s1,s2 ){ var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/; var ms = "" var formatedS1 = s1.replace(pattern, '$1-$2-$3 $4:$5:$6'); var formatedS2 = s2.replace(pattern, '$1-$2-$3 $4:$5:$6'); s1 = new Date(formatedS1.replace(/-/g, '/')); s2 = new Date(formatedS2.replace(/-/g, '/')); ms = Math.abs(s1.getTime() - s2.getTime()); return ms / 1000 / 60 / 60/24; }
function getDifDays( s1,s2 ){ var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/; var ms = "" s1 = new Date(s1.replace(pattern, '$1/$2/$3 $4:$5:$6')); s2 = new Date(s2.replace(pattern, '$1/$2/$3 $4:$5:$6')); ms = Math.abs(s1.getTime() - s2.getTime()); return ms / 1000 / 60 / 60/24; }
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(oDate1 - oDate2) / 1000 / 60 / 60 /24) //把相差的毫秒数转换为天数 return iDays + 1 }
来源:http://www.cnblogs.com/guoziyi/p/7058297.html