How to show the next session date depending on current date?

On my html website, I would like to show the date of the next lecture depending on the current data.

Let’s say, the following dates exist:

Calender week 42 of 2024: October 14, 2024

Calender week 43 of 2024: October 21, 2024

Calender week 3 of 2025: January 14, 2025

I added the following code to my html-file but that does not work.
Please note that I am completely new to this.

 <p id="demo"></p>
    <script>
      function getDateWeek(date) {
        const currentDate = 
            (typeof date === 'object') ? date : new Date();
        const januaryFirst = 
            new Date(currentDate.getFullYear(), 0, 1);
        const daysToNextMonday = 
            (januaryFirst.getDay() === 1) ? 0 : 
            (7 - januaryFirst.getDay()) % 7;
        const nextMonday = 
            new Date(currentDate.getFullYear(), 0, 
            januaryFirst.getDate() + daysToNextMonday);
     
        return (currentDate < nextMonday) ? 52 : 
        (currentDate > nextMonday ? Math.ceil(
        (currentDate - nextMonday) / (24 * 3600 * 1000) / 7) : 1);
    }
    </script>
    <script>
    const currentDate = new Date();
    const weekNumber = getDateWeek();
    let nextSession;
    switch (weekNumber) {
        case weekNumber <= 42:
          nextSession = "October 14, 2024";
          break;
        case weekNumber > 42 & weekNumber <= 43:
          nextSession = "October 21, 2024";
          break;
        case weekNumber > 43:
        nextSession = "January 14, 2025";
        break;
}
      document.getElementById("demo").innerHTML = "Nächster Termin: " + nextSession;
      </script>

You are on the right track a bit but some key issues which I notice are stated below

Issues

  1. Few key things- I could pinpoint is how you are using switch statements, switch statements don’t work with conditions such as you have provided, if/else would suffice you only use switch if you have a defined case like switch to case 1 or 2 etc.
    When to use switch cases; You can use it, if you have a simple option and you want to make reference to it multiple times.
    Such as
let selected_option = 2
switch (selected_option) {
  case 1:
    console.log('This item is not available');
    break;
  case 2:
    console.log('This item is available');
    break;
}
  1. Your use of bitwise operator & instead of logical &&(and): You should use &&

  2. You need to able to tell, how to calculate a week’s number, given year of interest.

Having said that , here is how to go about solving it, you might need to modify according to your preference but this holds true for all cases anyways. I have made the lectureDates an array of objects, so you can define what you want, because I don’t know how frequent they are. so I provided an array for you to modify instead.

Here is a code snippet to guide you and I also modified your code a bit

const lectureDates = [
  { week: 42, date: "October 14, 2024" },
  { week: 43, date: "October 21, 2024" },
  { week: 3, date: "January 14, 2025" },
];

function getWeekNumber(date) {
  const currentDate = date || new Date();
  const januaryFirst = new Date(currentDate.getFullYear(), 0, 1);
  const daysToNextMonday =
    januaryFirst.getDay() === 1 ? 0 : (7 - januaryFirst.getDay() + 1) % 7;
  const nextMonday = new Date(
    currentDate.getFullYear(),
    0,
    januaryFirst.getDate() + daysToNextMonday
  );
  const weekNumber =
    Math.ceil((currentDate - nextMonday) / (24 * 3600 * 1000) / 7) + 1;
  return weekNumber;
}

function getNextLectureDate(
  lectureDay,
  lectureStartHour,
  lectureEndHour,
  date
) {
  const currentDate = date || new Date();
  const currentWeek = getWeekNumber(currentDate);
  const currentDay = currentDate.getDay();
  const currentTime = currentDate.getHours() * 60 + currentDate.getMinutes();
  const lectureStartTime = lectureStartHour * 60;
  const lectureEndTime = lectureEndHour * 60;

  let nextSession = "No lectures today";

  for (const lecture of lectureDates) {
    const lectureDate = new Date(lecture.date);
    lectureDate.setHours(lectureEndHour, 0, 0, 0);

    if (currentDate.getTime() <= lectureDate.getTime()) {
      if (currentDate.toDateString() === lectureDate.toDateString()) {
        if (currentTime <= lectureEndTime) {
          nextSession = lecture.date;
          break;
        }
      } else {
        nextSession = lecture.date;
        break;
      }
    }
  }
  console.log(
    currentWeek,
    currentDay,
    lectureStartTime / 60,
    lectureEndTime / 60
  );
  return nextSession;
}

const lectureDay = 1; // Monday
const lectureStartHour = 15; // 3 PM
const lectureEndHour = 17; // 5 PM

console.log(getNextLectureDate(lectureDay, lectureStartHour, lectureEndHour));
console.log(
  getNextLectureDate(
    lectureDay,
    lectureStartHour,
    lectureEndHour,
    new Date("2024-10-21T14:30:00")
  )
);

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật