Files
illogical-impulse/dots/.config/quickshell/ii/modules/common/functions/DateUtils.qml
T
2026-03-08 18:09:34 +01:00

43 lines
1.2 KiB
QML

pragma Singleton
import Quickshell
Singleton {
id: root
function getFirstDayOfWeek(date, firstDay = 1) {
const d = new Date(date); // Copy
const day = d.getDay(); // 0 = Sunday, 1 = Monday, ..., 6 = Saturday
// 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());
}
function getIthDayDateOfSameWeek(date, i, firstDay = 1) {
const firstDayDate = root.getFirstDayOfWeek(date, firstDay);
const targetDate = new Date(firstDayDate);
targetDate.setDate(firstDayDate.getDate() + i);
return targetDate;
}
function formatDuration(seconds) {
const d = Math.floor(seconds / 86400);
const h = Math.floor((seconds % 86400) / 3600);
const m = Math.floor((seconds % 3600) / 60);
let str = "";
if (d > 0)
str += `${d}d`;
if (h > 0)
str += `${str ? ", " : ""}${h}h`;
if (m > 0 || !str)
str += `${str ? ", " : ""}${m}m`;
return str;
}
}