Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1773 Given a start date and a number of days can you calculate the end date?

You are here: irt.org | FAQ | JavaScript | Date | Q1773 [ previous next ]

Given a start date and number of days, the following gives you the end date?

function days_between(startdate,no_of_days) {
  var date  = startdate.substring(0,2);
  var month = startdate.substring(3,5);
  var year  = startdate.substring(6,10);
  startdate = month+"/"+date+"/"+year;

  var startdate = new Date(startdate)
  var curr_mon  = startdate.getMonth()+1;
  var next_mon  = curr_mon+1;

  dayIncrement=0;
  for (i=0;i<no_of_days;i++) {
    today_date = startdate.getDate();
    curr_mon = startdate.getMonth()+1;
    dayIncrement = today_date;

    if (curr_mon==next_mon) {
      next_mon = next_mon+1;
      dayIncrement=1;
    }
    if (startdate.getDay() == 0 || startdate.getDay() == 6) {
      no_of_days = no_of_days + 1;
    }
    if ((i+1)==no_of_days) {
      startdate.setDate(dayIncrement);
    } else {
      startdate.setDate(dayIncrement+1);
    }

  }

  var date = startdate.getDate();
  var month = startdate.getMonth()+1;
  var year = startdate.getYear();
  enddate = date+"/"+month+"/"+year;

  return enddate ;
}
</script>

Submitted by G.LaxmiNarayan

Feedback on 'Q1773 Given a start date and a number of days can you calculate the end date?'

©2018 Martin Webb