js计算日期相差天数

function DateDiff(sDate1, sDate2) { //sDate1和sDate2是2017-9-25格式 

    var aDate, oDate1, oDate2, iDays
    aDate = sDate1.split("-")
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为9-25-2017格式 
    aDate = sDate2.split("-")
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24) //把相差的毫秒数转换为天数 
    return iDays
}
 
//调用
s1  =  "2017-9-25" 
s2  =  "2017-1-5" 
alert("第一个日期;"+s1+"/n第二个日期:"+s2+"/n相差"+DateDiff(s1,s2)+"天")

  

 
来源:https://www.cnblogs.com/pingming/p/7595407.html

猜你喜欢

转载自www.cnblogs.com/tonnytong/p/9662504.html