lavva.exalushome
Version:
Library implementing communication and abstraction layers for ExalusHome system
38 lines • 1.53 kB
JavaScript
/**
* Utilities to test whether a given date falls within the current
* hour, day, week, month or year (local time – domyślnie strefa serwera,
* np. Europe/Warsaw gdy proces tam działa).
*/
export function isThisHour(date, now = new Date()) {
return date.getFullYear() === now.getFullYear() &&
date.getMonth() === now.getMonth() &&
date.getDate() === now.getDate() &&
date.getHours() === now.getHours();
}
export function isThisDay(date, now = new Date()) {
return date.getFullYear() === now.getFullYear() &&
date.getMonth() === now.getMonth() &&
date.getDate() === now.getDate();
}
/**
* @param weekStartsOn 0 = niedziela, 1 = poniedziałek (domyślnie).
*/
export function isThisWeek(date, now = new Date(), weekStartsOn = 1) {
// obliczamy początek tygodnia (00:00 lokalnego czasu)
const dayOfWeek = (now.getDay() + 7 - weekStartsOn) % 7;
const startOfWeek = new Date(now);
startOfWeek.setHours(0, 0, 0, 0);
startOfWeek.setDate(now.getDate() - dayOfWeek);
// koniec = początek + 7 dni
const endOfWeek = new Date(startOfWeek);
endOfWeek.setDate(startOfWeek.getDate() + 7);
return date >= startOfWeek && date < endOfWeek;
}
export function isThisMonth(date, now = new Date()) {
return date.getFullYear() === now.getFullYear() &&
date.getMonth() === now.getMonth();
}
export function isThisYear(date, now = new Date()) {
return date.getFullYear() === now.getFullYear();
}
//# sourceMappingURL=StatesHistoryHelper.js.map