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
- 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;
}
-
Your use of
bitwise
operator & instead of logical&&
(and): You should use&&
-
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