Skip to content Skip to sidebar Skip to footer

Google Apps Script: Number Of Days Between Two Dates After Subtracting Full Months

I have two dates. I would like to know a) the number of full months between the two dates and b) the number of days after subtracting the full months. Essentially what DATEDIF(

Solution 1:

To get the number of days after subtracting the full months, use this code:

functiongetNumberOfDaysBetweenTwoDatesSubstractFullMonths(startDate, endDate) {

  var startDay = startDate.getDate();
  var startMonth = startDate.getMonth();
  var startYear = startDate.getYear();

  var endDay = endDate.getDate();

  var startNumber = daysInMonth(startMonth, startYear) - startDay;
  var endNumber = endDay;

  return startNumber + endNumber;

}

functiondaysInMonth(month, year) {
  returnnewDate(year, month + 1, 0).getDate();
}

To check this in Google Sheets Script:

functionTEST_days() {
  var d1 = newDate('18 November 2015');
  var d2 = newDate('3 March 2016');

  Logger.log(getNumberOfDaysBetweenTwoDatesSubstractFullMonths(d1, d2));

}

Post a Comment for "Google Apps Script: Number Of Days Between Two Dates After Subtracting Full Months"