1

The requirement is to calculate the result of subtracting a certain number of days from a given date.

For example, if the given date is "3-Nov-2015 "and if the duration is 5 days, the result should be "29-Oct-2015".

What is the best way to do this?

Ashwin Balamohan
  • 3,303
  • 2
  • 25
  • 47
Roopa
  • 109
  • 8

1 Answers1

0

Using the momentjs library:

moment("3-Nov-2015", "DD-MMM-YYYY").subtract(5, 'days').format("DD-MMM-YYYY");

Or, more modularly:

function getPrevDate(startDate, numDays, dateFormat) {
  var dateFormat = dateFormat || "DD-MMM-YYYY"; // default date format
  return moment(startDate, dateFormat).subtract(numDays, 'days').format(dateFormat);
}

getPrevDate("3-Nov-2015", 5) returns "29-Oct-2015"

Ashwin Balamohan
  • 3,303
  • 2
  • 25
  • 47