waffles: calendar: generalize first day of week

This commit is contained in:
end-4
2025-11-27 13:17:24 +01:00
parent d40df98aa5
commit 0700e024d9
5 changed files with 26 additions and 31 deletions
@@ -4,35 +4,24 @@ import Quickshell
Singleton {
id: root
function getMonday(date, american = false) {
function getFirstDayOfWeek(date, firstDay = 1) {
const d = new Date(date); // Copy
const day = d.getDay(); // 0 = Sunday, 1 = Monday, ..., 6 = Saturday
// Calculate difference to Monday
if (american) {
// Week starts on Sunday
d.setDate(d.getDate() - day);
} else {
// Week starts on Monday
const diff = day === 0 ? -6 : 1 - day;
d.setDate(d.getDate() + diff);
}
// Calculate difference to firstDay
const diff = (day - firstDay + 7) % 7;
d.setDate(d.getDate() - diff);
return d;
}
function sameDate(d1, d2) {
return (
d1.getFullYear() === d2.getFullYear() &&
d1.getMonth() === d2.getMonth() &&
d1.getDate() === d2.getDate()
);
return (d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate());
}
function getIthDayDateOfSameWeek(date, i, american = false) {
const monday = root.getMonday(date, american);
const targetDate = new Date(monday);
targetDate.setDate(monday.getDate() + i);
function getIthDayDateOfSameWeek(date, i, firstDay = 1) {
const firstDayDate = root.getFirstDayOfWeek(date, firstDay);
const targetDate = new Date(firstDayDate);
targetDate.setDate(firstDayDate.getDate() + i);
return targetDate;
}
}