Files
workavia-calendar-front/src/utils/dateUtils.ts
T
lenhanphung 2686b13885 fix(calendar) #263 : calculate dynamic week range for month view data loading (#264)
- Calculate exact number of weeks needed instead of fixed 6 weeks
- Compute weeks based on actual days from startDate to lastOfMonth
2025-10-29 11:26:37 +01:00

70 lines
2.1 KiB
TypeScript

import moment from "moment";
export function formatDateToYYYYMMDDTHHMMSS(date: Date) {
return moment(date).format("YYYYMMDDTHHmmss");
}
export function getCalendarRange(date = new Date()) {
const year = date.getFullYear();
const month = date.getMonth();
const firstOfMonth = new Date(year, month, 1);
const dayOfWeekStart = firstOfMonth.getDay();
const diffToMonday = (dayOfWeekStart + 6) % 7;
const startDate = new Date(firstOfMonth);
startDate.setDate(firstOfMonth.getDate() - diffToMonday);
startDate.setHours(0, 0, 0, 0);
const lastOfMonth = new Date(year, month + 1, 0);
// Calculate how many days from startDate to lastOfMonth
const daysFromStart = Math.floor(
(lastOfMonth.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24)
);
// Calculate number of weeks needed to cover all days (round up)
const weeksNeeded = Math.ceil((daysFromStart + 1) / 7);
// endDate is the Sunday of the last week (startDate + weeks * 7 - 1)
const endDate = new Date(startDate);
endDate.setDate(startDate.getDate() + weeksNeeded * 7 - 1);
endDate.setHours(23, 59, 59, 999);
return {
start: startDate,
end: endDate,
};
}
export function getDeltaInMilliseconds(delta: {
years: number;
months: number;
days: number;
milliseconds: number;
}) {
const MS_PER_DAY = 24 * 60 * 60 * 1000;
const AVG_MS_PER_MONTH = 30.44 * MS_PER_DAY; // approx
const AVG_MS_PER_YEAR = 365.25 * MS_PER_DAY; // approx
return (
(delta.years || 0) * AVG_MS_PER_YEAR +
(delta.months || 0) * AVG_MS_PER_MONTH +
(delta.days || 0) * MS_PER_DAY +
(delta.milliseconds || 0)
);
}
export const computeStartOfTheWeek = (date: Date): Date => {
const startOfWeek = new Date(date);
startOfWeek.setDate(date.getDate() - ((date.getDay() + 6) % 7)); // Monday
startOfWeek.setHours(0, 0, 0, 0);
return startOfWeek;
};
export const computeWeekRange = (date: Date): { start: Date; end: Date } => {
const weekStart = computeStartOfTheWeek(date);
const weekEnd = new Date(weekStart);
weekEnd.setDate(weekStart.getDate() + 7);
return { start: weekStart, end: weekEnd };
};