0

I have an unknown range of days (based in two inputs from a calendar), Eg: it may start January 5 - 2009 and end November 19 - 2015. This gives me around 2,509 days.

I need to locate a fixed date (December 16) within the last year of that range to make an extraction in days and find how many days from the last December 16 to the end date of the input are.Heres a Diagram

I have my two dates converted in separated strings, year, month and day, also I have it as object.

Abiel Muren
  • 365
  • 1
  • 18
  • Duplicate of [*How do I get the number of days between two dates in JavaScript?*](http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript). – RobG Dec 23 '16 at 20:59
  • Have you looked into Moment.js (http://momentjs.com)? Its a pretty great library for working with dates, there's probably something there you can use. – jmona789 Dec 23 '16 at 21:09

1 Answers1

1

Would this work?

let endDate = new Date('Feb-23-2017');

for(let i = 0; i < 365; i++) {
  endDate.setDate(endDate.getDate() - 1);
  if(endDate.getDate() === 16 && endDate.getMonth() === 11) {
    alert(i + 1);
    break;
  }
}

Since you don't really care about the start date. Unless the range is less then a year AND the target day (dec 16) is not in that range. You can add checks for that.

Here's a plnkr that demonstrates that.

o4ohel
  • 1,779
  • 13
  • 12
  • I modified the end date so it could be compatible with my code, don't get all the logic behind this, but it worked, is not quite exact some times, I guess is the leap year. Thank you su much! – Abiel Muren Jan 08 '17 at 01:06