All-time date range logic
parent
52666b3013
commit
acb856a574
45
lib/date.js
45
lib/date.js
|
@ -35,10 +35,53 @@ export function getLocalTime(t) {
|
|||
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 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;
|
||||
|
||||
if (+num === 1) {
|
||||
|
|
Loading…
Reference in New Issue