jquery 根据开始日、会话数、天数间隔(常规、间隔日)计算结束日期

qcuzuvrc  于 2023-08-04  发布在  jQuery
关注(0)|答案(2)|浏览(103)

我想根据会话开始日期、会话数量、治疗日(常规或交替日)和周日休息(周日无治疗)计算结束日期。在此代码中,未正确计算结束日期。参考请看图片。

在此开始日期01-08-2023中,常规日,会话为6,计算结束日期为06-08-2023,即周日

x1c 0d1x的数据

在此开始日期01-08-2023中,常规日,会话为10,它计算的结束日期为12-08-2023而不是11,因为只有1个星期日


same in alternate days
这是我的HTML代码。此代码的结果显示在上面的图像中

<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>`

字符串
这是javasript代码

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;
}

mctunoxg

mctunoxg1#

我已经调整了你的计算函数,以获得sessionEndDate。我已经做了全面检查,请在使用前仔细检查。
checkout 链接here

jaql4c8m

jaql4c8m2#

这个功能为我工作@jignesh感谢您的帮助。

function calculateEndDate(startDate, noOfSessions, therapyDay) {
  const sessionDates = [];
  let currentDate = new Date(startDate);

  for (let i = 0; i < noOfSessions; i++) {
    if (therapyDay === 'Regular' || therapyDay === 'NonRegular') {
      // Add session for regular or non-regular therapy days
      sessionDates.push(new Date(currentDate));
    }

    // Increment the date for the next session
    currentDate.setDate(currentDate.getDate() + 1);
  }

  // Calculate the end date by adding the last session's duration
  const lastSessionDuration = therapyDay === 'Regular' ? 1 : 2;
  const addNoofDays = (noOfSessions - 1) * lastSessionDuration;

  let endDate = new Date(startDate);
  endDate.setDate(endDate.getDate() + addNoofDays);

  let sundayCnt = 0;

  for (const sessionDate of sessionDates) {
    if (sessionDate.getDay() === 0) {
      sundayCnt++;
    }
  }

  endDate.setDate(startDate.getDate() + addNoofDays + sundayCnt);

  // check if end date falls on Sunday then add one more day
  if (endDate.getDay() === 0) {
    sundayCnt++;
  }

  endDate.setDate(startDate.getDate() + addNoofDays + sundayCnt);
  return endDate;
  }

  // Example usage for non-regular session dates (1,3,5,7,9,11,14):
  const sessionEndDate = calculateEndDate(new Date('2023-07-21'), 4, 
  'NonRegular');
  console.log('sessionEndDate', sessionEndDate);

字符串

相关问题