forked from Github/frigate
Add ability to filter search by time range (#13946)
* Add ability to filter by time range * Cleanup * Handle input with tags * fix input for time_range filter * fix before and after filters * clean up * Ensure the default value works as expected * Handle time range in am/pm based on browser * Fix arrow * Fix text * Handle midnight case * fix width * Fix bg * Fix bg * Fix mobile spacing * y spacing * remove left padding --------- Co-authored-by: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com>
This commit is contained in:
@@ -373,3 +373,48 @@ export function getIntlDateFormat() {
|
||||
}, [] as string[])
|
||||
.join("");
|
||||
}
|
||||
|
||||
export function formatDateToLocaleString(daysOffset: number = 0): string {
|
||||
const date = new Date();
|
||||
date.setDate(date.getDate() + daysOffset);
|
||||
|
||||
return new Intl.DateTimeFormat(window.navigator.language, {
|
||||
day: "2-digit",
|
||||
month: "2-digit",
|
||||
year: "numeric",
|
||||
})
|
||||
.format(date)
|
||||
.replace(/[^\d]/g, "");
|
||||
}
|
||||
|
||||
export function isValidTimeRange(rangeString: string): boolean {
|
||||
const range = rangeString.split(",");
|
||||
|
||||
if (range.length !== 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const toMinutes = (time: string): number => {
|
||||
const [h, m] = time.split(":").map(Number);
|
||||
return h * 60 + m;
|
||||
};
|
||||
|
||||
const isValidTime = (time: string): boolean =>
|
||||
/^(?:([01]\d|2[0-3]):([0-5]\d)|24:00)$/.test(time);
|
||||
|
||||
const [startTime, endTime] = range;
|
||||
|
||||
return (
|
||||
isValidTime(startTime) &&
|
||||
isValidTime(endTime) &&
|
||||
toMinutes(startTime) < toMinutes(endTime)
|
||||
);
|
||||
}
|
||||
|
||||
export function convertTo12Hour(time: string) {
|
||||
const [hours, minutes] = time.split(":");
|
||||
const hour = parseInt(hours, 10);
|
||||
const ampm = hour >= 12 ? "PM" : "AM";
|
||||
const hour12 = hour % 12 || 12;
|
||||
return `${hour12}:${minutes} ${ampm}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user