根据起始日期、会话数量和天数间隔(正常、交替日)计算结束日期。

huangapple go评论84阅读模式
英文:

Calculate end date based on start day, no of session, Days Interval (Regular, alternate days)

问题

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

根据起始日期、会话数量和天数间隔(正常、交替日)计算结束日期。 same in alternate days

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 JavaScript 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;
}
英文:

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

根据起始日期、会话数量和天数间隔(正常、交替日)计算结束日期。
same in alternate days

This is my html code. results of this code is shown in above images

&lt;div class=&quot;col-12 col-md-6 col-xl-4&quot;&gt;
&lt;div class=&quot;form-group local-forms&quot;&gt;
&lt;label for=&quot;therapyday&quot;&gt;Therapy Day &lt;span class=&quot;login-danger&quot;&gt;*&lt;/span&gt;&lt;/label&gt;
&lt;select id=&quot;therapyday&quot; name=&quot;therapyday&quot; class=&quot;form-control&quot; required&gt;
&lt;option value=&quot;Regular&quot;&gt;Regular&lt;/option&gt;
&lt;option value=&quot;Alternate Day&quot;&gt;Alternate Day&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-12 col-md-6 col-xl-4&quot;&gt;
&lt;div class=&quot;form-group local-forms&quot;&gt;
&lt;label for=&quot;noSession&quot;&gt;No. of Session &lt;span class=&quot;login-danger&quot;&gt;*&lt;/span&gt;&lt;/label&gt;
&lt;input id=&quot;noSession&quot; name=&quot;noSession&quot; class=&quot;form-control&quot; type=&quot;text&quot; required&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-12 col-md-6 col-xl-4&quot;&gt;
&lt;div class=&quot;form-group local-forms&quot;&gt;
&lt;label for=&quot;sessionstartdate&quot;&gt;Session Start Date&lt;span class=&quot;login-danger&quot;&gt;*&lt;/span&gt;&lt;/label&gt;
&lt;input id=&quot;sessionstartdate&quot; name=&quot;sessionstartdate&quot; class=&quot;form-control&quot; type=&quot;date&quot; required&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-12 col-md-6 col-xl-4&quot;&gt;
&lt;div class=&quot;form-group local-forms&quot;&gt;
&lt;label for=&quot;sessionenddate&quot;&gt;Session End Date&lt;span class=&quot;login-danger&quot;&gt;*&lt;/span&gt;&lt;/label&gt;
&lt;input id=&quot;sessionenddate&quot; name=&quot;sessionenddate&quot; class=&quot;form-control&quot; type=&quot;date&quot; required readonly&gt;
&lt;/div&gt;
&lt;/div&gt;`

this is javasript code

const therapyDaySelect = document.getElementById(&quot;therapyday&quot;);
const noSessionInput = document.getElementById(&quot;noSession&quot;);
const sessionStartDateInput = document.getElementById(&quot;sessionstartdate&quot;);
const sessionEndDateInput = document.getElementById(&quot;sessionenddate&quot;);
// Add event listeners to calculate session end date
therapyDaySelect.addEventListener(&quot;change&quot;, calculateSessionEndDate);
noSessionInput.addEventListener(&quot;input&quot;, calculateSessionEndDate);
sessionStartDateInput.addEventListener(&quot;change&quot;, 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(&quot;T&quot;)[0];
}
function calculateEndDate(startDate, noOfSessions, therapyDay) {
const sessionDuration = therapyDay === &quot;Regular&quot; ? 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 === &quot;Regular&quot;
? adjustedSessions
: Math.min(adjustedSessions, startDayOffset);
endDate.setDate(endDate.getDate() + additionalDays);
return endDate;
}

答案1

得分: 0

我已调整了您的计算函数,以获取sessionEndDate。
我已进行了健全性检查,请在使用之前再次检查。

查看链接此处

英文:

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

答案2

得分: 0

这个函数对我有用,@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);

这是您提供的代码的翻译。

英文:

This function worked for me @jignesh thanks for your help.

function calculateEndDate(startDate, noOfSessions, therapyDay) {
const sessionDates = [];
let currentDate = new Date(startDate);
for (let i = 0; i &lt; noOfSessions; i++) {
if (therapyDay === &#39;Regular&#39; || therapyDay === &#39;NonRegular&#39;) {
// 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&#39;s duration
const lastSessionDuration = therapyDay === &#39;Regular&#39; ? 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(&#39;2023-07-21&#39;), 4, 
&#39;NonRegular&#39;);
console.log(&#39;sessionEndDate&#39;, sessionEndDate);

huangapple
  • 本文由 发表于 2023年7月18日 13:35:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定