demonz's avatar

Urgent calculate the end date from start date and some values

Hi im using react js and I have for example start date 15/1/2023 a lesson duration = 100 hours , each week the lesson is taught on sunday for 2 hours and for monday 2 hours , calculate the end date so it finishes the total lesson hours

0 likes
2 replies
AddWebContribution's avatar
import React, { useState } from 'react';

const LessonEndDate = () => {
  const startDate = new Date(2023, 0, 15); // Jan 15, 2023
  const lessonDuration = 100; // 100 hrs
  const weeklySchedule = [
    {day: 0, hours: 2}, // sunday
    {day: 1, hours: 2}  // monday
  ];

  const [endDate, setEndDate] = useState(new Date(startDate));
  let totalHours = 0;

  while (totalHours < lessonDuration) {
    for (const {day, hours} of weeklySchedule) {
      const daysToAdd = (day - endDate.getDay() + 7) % 7;
      setEndDate(new Date(endDate.setDate(endDate.getDate() + daysToAdd)));
      totalHours += hours;
      if (totalHours >= lessonDuration) {
        break;
      }
    }
  }

  return (
    <div>
      <p>End Date: {endDate.toLocaleDateString()}</p>
    </div>
  );
};

export default LessonEndDate;

the component uses the useState hook to store the end date, which is updated in the while loop. The setEndDate function is used to update the end date. The component returns a div element with a paragraph that displays the end date in a localized date string format.

1 like
hupp's avatar

you can calculate the end date of the lesson by creating a function that takes the start date and lesson duration as inputs, and returns the end date as output. Here's an example implementation:

import React from 'react';

function LessonEndDate({ startDate, lessonDuration }) {
  // Step 1: Calculate the total number of weeks needed to complete the lesson
  const weeksNeeded = Math.ceil(lessonDuration / 4); // 4 hours of class per week

  // Step 2: Determine the date of the first Sunday
  const firstSunday = new Date(startDate);
  while (firstSunday.getDay() !== 0) { // Sunday = 0
    firstSunday.setDate(firstSunday.getDate() + 1);
  }

  // Step 3: Determine the date of the first Monday
  const firstMonday = new Date(startDate);
  while (firstMonday.getDay() !== 1) { // Monday = 1
    firstMonday.setDate(firstMonday.getDate() + 1);
  }

  // Step 4: Calculate the date of the last Sunday
  const lastSunday = new Date(firstSunday.getTime() + (weeksNeeded - 1) * 7 * 24 * 60 * 60 * 1000);

  // Step 5: Calculate the date of the last Monday
  const lastMonday = new Date(firstMonday.getTime() + (weeksNeeded - 1) * 7 * 24 * 60 * 60 * 1000 + 24 * 60 * 60 * 1000);

  // Return the later of the two dates
  return lastSunday > lastMonday ? lastSunday.toLocaleDateString() : lastMonday.toLocaleDateString();
}

export default LessonEndDate;

This function uses the JavaScript Date object to perform the date calculations. The getDate() and setDate() methods are used to navigate to the first Sunday and Monday of the lesson. The getTime() method is used to convert the dates to milliseconds so that we can add the necessary time intervals to calculate the dates of the last Sunday and Monday. Finally, the toLocaleDateString() method is used to format the date as a string in the user's locale.

1 like

Please or to participate in this conversation.