presentdate = "11/30/2018" and enddate = "1/29/2019", then newdate = "12/30/2018"
/Step1: var sDt = new Date(startDate); var pDt = new Date(presentDate); //Step2: Doing Step 3 directly returns invalid date so using the following step worked in returning dates var presentDt = (pDt.getMonth() + 1) + '/' + pDt.getDate() + '/' + pDt.getFullYear(); var sDt = (sDt.getMonth() + 1) + '/' + sDt.getDate() + '/' + sDt.getFullYear(); //Step 3 var mPresent = moment(presentDt, "MM/DD/YYYY"); var mStart = moment(sDt, "MM/DD/YYYY"); //Step 4 // Create a new momnt object compliant with your needs var pastStart = moment({ year: mPresent.year(), // get presentDt's year month: mPresent.add(1, 'month').month(), // get presentDt's month date: mStart.add(1, 'day').date() // get startdt day of the month and add 1 day to it }); // Display the result in the format you need console.log(pastStart.format("MM/DD/YYYY")); });
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)