2026-06-10 19:28:54 -05:00
|
|
|
/**
|
|
|
|
|
* Server-side local-date helpers.
|
|
|
|
|
*
|
|
|
|
|
* Bill due dates and payment dates are stored as plain YYYY-MM-DD strings in
|
|
|
|
|
* the server's local timezone (the client computes "today" the same way in
|
2026-07-05 13:48:44 -05:00
|
|
|
* client/pages/TrackerPage.tsx → localDateString). Never derive a calendar
|
2026-06-10 19:28:54 -05:00
|
|
|
* date from Date.toISOString() — that yields the UTC date, which disagrees
|
|
|
|
|
* with local time around midnight and month boundaries.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** YYYY-MM-DD in local time. */
|
2026-07-05 13:48:44 -05:00
|
|
|
export function localDateString(date: Date = new Date()): string {
|
2026-06-10 19:28:54 -05:00
|
|
|
const year = date.getFullYear();
|
|
|
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
|
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
|
|
|
return `${year}-${month}-${day}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** { year, month } (1-based month) in local time. */
|
2026-07-05 13:48:44 -05:00
|
|
|
export function localYearMonth(date: Date = new Date()): { year: number; month: number } {
|
2026-06-10 19:28:54 -05:00
|
|
|
return { year: date.getFullYear(), month: date.getMonth() + 1 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** YYYY-MM-DD in local time, `days` days before `from`. */
|
2026-07-05 13:48:44 -05:00
|
|
|
export function localDateStringDaysAgo(days: number, from: Date = new Date()): string {
|
2026-06-10 19:28:54 -05:00
|
|
|
const d = new Date(from);
|
|
|
|
|
d.setDate(d.getDate() - days);
|
|
|
|
|
return localDateString(d);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-10 19:42:51 -05:00
|
|
|
/** Today as YYYY-MM-DD in local time. Alias of localDateString(). */
|
2026-07-05 13:48:44 -05:00
|
|
|
export function todayLocal(): string {
|
2026-06-10 19:42:51 -05:00
|
|
|
return localDateString(new Date());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** YYYY-MM month key in local time (e.g. "2026-06"). */
|
2026-07-05 13:48:44 -05:00
|
|
|
export function monthKey(date: Date = new Date()): string {
|
2026-06-10 19:42:51 -05:00
|
|
|
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`;
|
|
|
|
|
}
|