refactor: use date-fns for dashboard dates

This commit is contained in:
2026-06-25 11:48:18 +01:00
parent fe083ffaea
commit 3642ecfba6
3 changed files with 18 additions and 27 deletions
+6 -27
View File
@@ -1,3 +1,4 @@
import { addDays, addMonths, endOfMonth, format, parseISO, startOfMonth } from 'date-fns';
import { and, asc, count, desc, eq, gte, inArray, isNull, lt, lte, ne, sql } from 'drizzle-orm';
import { db } from '$lib/server/db';
import {
@@ -12,42 +13,20 @@ import {
import { loadOrganizationContext } from '$lib/server/organizations';
import type { PageServerLoad } from './$types';
const dayMs = 24 * 60 * 60 * 1000;
function pad(value: number) {
return String(value).padStart(2, '0');
}
function dateOnly(date: Date) {
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
return format(date, 'yyyy-MM-dd');
}
function dateTimeLocal(date: Date) {
return `${dateOnly(date)}T${pad(date.getHours())}:${pad(date.getMinutes())}`;
}
function startOfMonth(date: Date) {
return new Date(date.getFullYear(), date.getMonth(), 1);
}
function endOfMonth(date: Date) {
return new Date(date.getFullYear(), date.getMonth() + 1, 0);
}
function addDays(date: Date, days: number) {
return new Date(date.getTime() + days * dayMs);
}
function addMonths(date: Date, months: number) {
return new Date(date.getFullYear(), date.getMonth() + months, 1);
return format(date, "yyyy-MM-dd'T'HH:mm");
}
function monthKey(date: Date) {
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}`;
return format(date, 'yyyy-MM');
}
function monthLabel(date: Date) {
return new Intl.DateTimeFormat('en-GB', { month: 'short', year: 'numeric' }).format(date);
return format(date, 'MMM yyyy');
}
function roomTypeLabel(type: string) {
@@ -323,7 +302,7 @@ export const load: PageServerLoad = async ({ locals }) => {
});
}
for (const invoice of billingTrendInvoiceRows) {
const issuedAt = new Date(`${invoice.issueDate}T00:00:00`);
const issuedAt = parseISO(invoice.issueDate);
const key = monthKey(issuedAt);
const existing = billingTrendMap.get(key);
if (!existing) continue;