Updated date range handling.

This commit is contained in:
Mike Cao 2023-05-28 22:28:11 -07:00
parent bfb52eb678
commit e9b0d3f796
4 changed files with 25 additions and 15 deletions

View file

@ -40,20 +40,27 @@ export function getLocalTime(t) {
export function parseDateRange(value, locale = 'en-US') {
if (typeof value === 'object') {
const { startDate, endDate } = value;
return value;
}
if (value?.startsWith?.('range')) {
const [, startAt, endAt] = value.split(':');
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
return {
...value,
startDate: typeof startDate === 'string' ? parseISO(startDate) : startDate,
endDate: typeof endDate === 'string' ? parseISO(endDate) : endDate,
...getDateRangeValues(startDate, endDate),
value,
};
}
const now = new Date();
const dateLocale = getDateLocale(locale);
const match = value.match(/^(?<num>[0-9-]+)(?<unit>hour|day|week|month|year)$/);
const match = value?.match?.(/^(?<num>[0-9-]+)(?<unit>hour|day|week|month|year)$/);
if (!match) return {};
if (!match) return null;
const { num, unit } = match.groups;