Standardize time formatting and avoid string slicing

- Created formatTimeString() utility to properly parse time strings
- Replaced all .slice(0, 5) calls with proper time formatting
- Handles both 'HH:MM:SS' and 'HH:MM' formats safely
- More robust than string slicing
This commit is contained in:
counterweight 2025-12-21 17:56:20 +01:00
parent a25e84e197
commit 3c13270bc0
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
2 changed files with 17 additions and 4 deletions

View file

@ -46,6 +46,19 @@ export function formatDisplayDate(d: Date): string {
return d.toLocaleDateString("en-US", { 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.