All-time date range logic

pull/882/head
Chris Walsh 2021-12-04 02:07:15 -08:00
parent 52666b3013
commit acb856a574
No known key found for this signature in database
GPG Key ID: 28EE0CCA6032019E
1 changed files with 44 additions and 1 deletions

View File

@ -35,10 +35,53 @@ export function getLocalTime(t) {
return addMinutes(new Date(t), new Date().getTimezoneOffset()); return addMinutes(new Date(t), new Date().getTimezoneOffset());
} }
export function getDateRange(value, locale = 'en-US') { export function getDateRange(value, locale = 'en-US', createdAt = null) {
const now = new Date(); const now = new Date();
const dateLocale = getDateLocale(locale); const dateLocale = getDateLocale(locale);
if (value === 'all') {
createdAt = new Date(createdAt);
const diff = Math.abs(differenceInCalendarDays(createdAt, now));
if (createdAt) {
if (diff <= 1) {
return {
startDate: startOfDay(createdAt),
endDate: endOfDay(now),
unit: 'hour',
value,
};
} else if (diff <= 90 && diff > 1) {
return {
startDate: startOfWeek(createdAt),
endDate: endOfWeek(now),
unit: 'day',
value,
};
} else if (diff <= 1095 && diff > 90) {
return {
startDate: startOfMonth(createdAt),
endDate: endOfMonth(now),
unit: 'month',
value,
};
} else if (diff > 1095) {
return {
startDate: startOfYear(createdAt),
endDate: endOfYear(now),
unit: 'year',
value,
};
}
}
return {
startDate: startOfYear(createdAt),
endDate: endOfYear(now),
unit: 'year',
value,
};
}
const { num, unit } = value.match(/^(?<num>[0-9]+)(?<unit>hour|day|week|month|year)$/).groups; const { num, unit } = value.match(/^(?<num>[0-9]+)(?<unit>hour|day|week|month|year)$/).groups;
if (+num === 1) { if (+num === 1) {