Click here to Skip to main content
13,799,870 members
Rate this:
 
Please Sign up or sign in to 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 6-Dec-18 8:00am
Updated 6-Dec-18 14: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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.181214.1 | Last Updated 6 Dec 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100