I want to calculate end date based on session start date, no of session, therapy days (Regular or Alternate days) and Sunday is off(no therapies on Sunday). In this code end date is not calculating properly. for references please see the images.
In this start date 01-08-2023, regular days, session are 6 and it is calculating end date as 06-08-2023 which is sunday
In this start date 01-08-2023, regular days, session are 10 and it is calculating end date as 12-08-2023 instead of 11 as there is only 1 sunday
This is my html code. results of this code is shown in above images
<div class="col-12 col-md-6 col-xl-4">
<div class="form-group local-forms">
<label for="therapyday">Therapy Day <span class="login-danger">*</span></label>
<select id="therapyday" name="therapyday" class="form-control" required>
<option value="Regular">Regular</option>
<option value="Alternate Day">Alternate Day</option>
</select>
</div>
</div>
<div class="col-12 col-md-6 col-xl-4">
<div class="form-group local-forms">
<label for="noSession">No. of Session <span class="login-danger">*</span></label>
<input id="noSession" name="noSession" class="form-control" type="text" required>
</div>
</div>
<div class="col-12 col-md-6 col-xl-4">
<div class="form-group local-forms">
<label for="sessionstartdate">Session Start Date<span class="login-danger">*</span></label>
<input id="sessionstartdate" name="sessionstartdate" class="form-control" type="date" required>
</div>
</div>
<div class="col-12 col-md-6 col-xl-4">
<div class="form-group local-forms">
<label for="sessionenddate">Session End Date<span class="login-danger">*</span></label>
<input id="sessionenddate" name="sessionenddate" class="form-control" type="date" required readonly>
</div>
</div>`
this is javasript code
const therapyDaySelect = document.getElementById("therapyday");
const noSessionInput = document.getElementById("noSession");
const sessionStartDateInput = document.getElementById("sessionstartdate");
const sessionEndDateInput = document.getElementById("sessionenddate");
// Add event listeners to calculate session end date
therapyDaySelect.addEventListener("change", calculateSessionEndDate);
noSessionInput.addEventListener("input", calculateSessionEndDate);
sessionStartDateInput.addEventListener("change", calculateSessionEndDate);
function calculateSessionEndDate() {
// Retrieve user input values
const therapyDay = therapyDaySelect.value;
const noSession = parseInt(noSessionInput.value);
const sessionStartDate = new Date(sessionStartDateInput.value);
// Calculate session end date
const sessionEndDate = calculateEndDate(
sessionStartDate,
noSession,
therapyDay
);
// Set session end date as the value of the input field
sessionEndDateInput.value = sessionEndDate.toISOString().split("T")[0];
}
function calculateEndDate(startDate, noOfSessions, therapyDay) {
const sessionDuration = therapyDay === "Regular" ? 1 : 2;
const endDate = new Date(startDate);
endDate.setDate(startDate.getDate() + noOfSessions * sessionDuration - 1);
const startDay = startDate.getDay();
const startDayOffset = startDay === 0 ? 1 : 7 - startDay;
const adjustedSessions = Math.floor((noOfSessions - 1) / 7) * 2;
const additionalDays =
therapyDay === "Regular"
? adjustedSessions
: Math.min(adjustedSessions, startDayOffset);
endDate.setDate(endDate.getDate() + additionalDays);
return endDate;
}
2
Answers
This function worked for me @jignesh thanks for your help.
I’ve tweaked your calculation function, to get the sessionEndDate.
I’ve done sanity check, pls do double check before using it.
checkout the link here