83 lines
2.2 KiB
TypeScript
83 lines
2.2 KiB
TypeScript
/**
|
|
* Shared date formatting utilities.
|
|
*/
|
|
|
|
/**
|
|
* Format date as YYYY-MM-DD in local timezone.
|
|
*/
|
|
export function formatDate(d: Date): string {
|
|
const year = d.getFullYear();
|
|
const month = String(d.getMonth() + 1).padStart(2, "0");
|
|
const day = String(d.getDate()).padStart(2, "0");
|
|
return `${year}-${month}-${day}`;
|
|
}
|
|
|
|
/**
|
|
* Format time from ISO string to HH:MM format in local timezone.
|
|
*/
|
|
export function formatTime(isoString: string): string {
|
|
const d = new Date(isoString);
|
|
return d.toLocaleTimeString("es-ES", {
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
hour12: false,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Format datetime from ISO string to a readable format in local timezone.
|
|
*/
|
|
export function formatDateTime(isoString: string, locale: string = "es-ES"): string {
|
|
const d = new Date(isoString);
|
|
return d.toLocaleString(locale, {
|
|
weekday: "short",
|
|
month: "short",
|
|
day: "numeric",
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
hour12: false,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Format date for display (e.g., "Mon, Jan 15").
|
|
*/
|
|
export function formatDisplayDate(d: Date): string {
|
|
return d.toLocaleDateString("es-ES", { weekday: "short", month: "short", day: "numeric" });
|
|
}
|
|
|
|
/**
|
|
* Format time string from "HH:MM:SS" or "HH:MM" format to "HH:MM".
|
|
* Avoids string slicing by properly parsing the time.
|
|
*/
|
|
export function formatTimeString(timeStr: string): string {
|
|
// Handle both "HH:MM:SS" and "HH:MM" formats
|
|
const parts = timeStr.split(":");
|
|
if (parts.length >= 2) {
|
|
return `${parts[0].padStart(2, "0")}:${parts[1].padStart(2, "0")}`;
|
|
}
|
|
return timeStr;
|
|
}
|
|
|
|
/**
|
|
* Get date range for booking/availability (from minAdvanceDays to maxAdvanceDays).
|
|
* Returns an array of Date objects starting from minAdvanceDays days from today.
|
|
*/
|
|
export function getDateRange(minAdvanceDays: number, maxAdvanceDays: number): Date[] {
|
|
const dates: Date[] = [];
|
|
const today = new Date();
|
|
for (let i = minAdvanceDays; i <= maxAdvanceDays; i++) {
|
|
const d = new Date(today);
|
|
d.setDate(today.getDate() + i);
|
|
dates.push(d);
|
|
}
|
|
return dates;
|
|
}
|
|
|
|
/**
|
|
* Check if a date is a weekend (Saturday or Sunday).
|
|
*/
|
|
export function isWeekend(date: Date): boolean {
|
|
const day = date.getDay();
|
|
return day === 0 || day === 6;
|
|
}
|