JS 計算幾個月後的日期 與 兩個日期相差幾個月

2021-10-08 19:58:05 字數 1653 閱讀 2327

/**

* @description: 獲取n個月後的日期

* @param dtstr 日期

* @param n 幾個月

* @return: 日期

*/addmulmonth(dtstr, n)else

//獲取當前日期中月的天數

let newdate = new date(yy,mm,0)

//這裡傳入的0代表獲取上乙個月的最後一天;如果傳1,則獲得當月的第一天

let days = newdate.getdate()

//天數大於當前月

if(dd > days)

return yy + "-" + mm + "-" + dd

}this.addmulmonth('2020-1-31',1)

//2020-2-31

/**

* @description: 獲取n個月後的日期

* @param n 幾個月

* @return: 日期

*/addmulmonth(n)else

//獲取當前日期中月的天數

let newdate = new date(yy,mm,0)

//這裡傳入的0代表獲取上乙個月的最後一天;如果傳1,則獲得當月的第一天

let days = newdate.getdate()

if(dd > days)

return yy + "-" + mm + "-" + dd

}console.log(this.addmulmonth(6))

/**

* @description: 計算指定日期與現在相差多少個月

* @param compareyear 年

* @param comparemonth 月

* @param compareday 日

* @return: 相差的月數

*/getdistancemonth(compareyear,comparemonth,compareday)else

return yeartomonth + monthtomonth + datetomonth

}this.getdistancemonth(2020,9,2)

// 1

/**

* @description: 計算兩個日期相差多少個月

* @param starttime 開始時間

* @param endtime 結束時間

* @return: 相差的月數

*/getdistancemonth(starttime,endtime)else

let yeartomonth = (endtime.getyear() - starttime.getyear()) * 12

let monthtomonth = endtime.getmonth() - starttime.getmonth()

return yeartomonth + monthtomonth + datetomonth}

console.log(this.getdistancemonth('2020/1/7','2020/5/7'))

// 4

計算兩個日期相差幾年幾個月

public static class datetimediff catch 計算日期間隔 要參與計算的其中乙個日期 要參與計算的另乙個日期 乙個表示日期間隔的timespan型別 public static timespan toresult datetime d1,datetime d2 els...

C 日期間隔 如何計算兩個日期相差幾年幾個月?

計算日期的間隔 靜態類 public static class datetimediff catch 計算日期間隔 要參與計算的其中乙個日期 要參與計算的另乙個日期 乙個表示日期間隔的timespan型別 public static timespan toresult datetime d1,dat...

js中 計算兩個日期間的工作日

起始日期,pattern 是正規表示式的界定符,pattern是要匹配的內容,只用於第乙個符號的匹配,g為全域性匹配標誌 var begindate new date 2013 01 01 replace g,結束日期 var enddate new date 2013 01 31 replace ...