Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 2 dates and I need to create a 3rd date using those 2 dates.
If
presentdate = "11/30/2018" and enddate = "1/29/2019", then newdate = "12/30/2018"


Since, JavaScript date months range from 0-11 and also the dates are kind of messy, I am not getting the desired results.

What I have tried:

/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"));

});



This also returns invalid date if my date is "1/30/2019" and endate ="5/30/2019" because when you take end day + 1 and create new Date February doesn't have 31 days. And itr returns Invalid date.

Any help is appreciated. Thank you.
Posted
Updated 6-Dec-18 13:49pm
v2
Comments
MadMyche 6-Dec-18 13:50pm    
Where is "endDay" defined? And what EXACTLY to you want the "next date" to be relative to the 2 dates input?
Member 14013562 6-Dec-18 19:49pm    
Updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900