});
}
-var date_being_displayed;
-var days_being_displayed = 7; /* 1 = day view; 5 = work week view; 7 = week view */
-var start_of_week = 0; /* 0 = Sunday; 1 = Monday */
+date_being_displayed = new Date();
+days_being_displayed = 7; /* 1 = day view; 5 = work week view; 7 = week view */
+start_of_week = 1; /* 0 = Sunday; 1 = Monday */
// Update the calendar display (we might have changed dates, or added/removed data)
function update_calendar_display() {
- let this_column_date = date_being_displayed;
+ // Clone the "date_being_displayed" and scope it locally for this function.
+ let this_column_date = new Date(date_being_displayed);
- // If displaying a whole week, start the week on the correct day.
+ // If displaying a whole week, start the week on the correct day of the week (usually Sunday or Monday)
if (days_being_displayed == 7) {
while (this_column_date.getDay() != start_of_week) {
this_column_date.setDate(this_column_date.getDate() - 1);
}
}
+ // If displaying a work week, start the week on Monday
+ if (days_being_displayed == 5) {
+ while (this_column_date.getDay() != 1) {
+ this_column_date.setDate(this_column_date.getDate() - 1);
+ }
+ }
+
// Go through every day on the screen (up to a week I guess)
for (let i=0; i<days_being_displayed; ++i) {
- // Get y-m-d to display
- let day_of_month = this_column_date.getDate();
- let month = this_column_date.getMonth() + 1;
- let year = this_column_date.getFullYear();
-
+
// Populate the columns
- var weekday = this_column_date.toLocaleString("default", { weekday: "short" })
+ let year = this_column_date.getFullYear();
+ let month = this_column_date.getMonth() + 1;
+ let day_of_month = this_column_date.getDate();
+ let weekday = this_column_date.toLocaleString("default", { weekday: "short" })
document.getElementById("ctdl-cal-day" + i).innerHTML = weekday + " " + year + "-" + month + "-" + day_of_month;
// Get ready for the next column
// Go back by one week.
function go_back_one_week() {
- date_being_displayed.setDate(date_being_displayed.getDate() - 8 ); // why 8 instead of 7?
+ date_being_displayed.setDate(date_being_displayed.getDate() - 7 );
update_calendar_display();
}
// Advance the calendar by one week.
function go_forward_one_week() {
- date_being_displayed.setDate(date_being_displayed.getDate() + 6); // why 6 instead of 7?
+ date_being_displayed.setDate(date_being_displayed.getDate() + 7);
update_calendar_display();
}