計算天數差,日期格式
阿新 • • 發佈:2018-12-30
//計算天數差的函式,通用 function DateDiff(sDate1, sDate2){ //sDate1和sDate2是2002-12-18格式 var aDate, oDate1, oDate2, iDays aDate = sDate1.split("-") oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //轉換為12-18-2002格式 aDate = sDate2.split("-") oDate2 = new Date(aDate[1] + '-'+ aDate[2] + '-' + aDate[0]) iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24) //把相差的毫秒數轉換為天數 return iDays } //今天日期的標準格式 function getToday(){ var today = new Date(); var year = today.getFullYear(); var month = today.getMonth()+1; var day = today.getDate(); vardate = year+"-"+month+"-"+day; console.log("今天的日期"+date); return date; } //將資料庫的日期資料改為2002-12-18格式 function dbdate(s){ var date = s.split(" "); console.log("最後借閱日期:"+date[0]); return date[0]; }