From acb856a5748bb02ee06d7f602432e72e79565e45 Mon Sep 17 00:00:00 2001 From: Chris Walsh Date: Sat, 4 Dec 2021 02:07:15 -0800 Subject: [PATCH] All-time date range logic --- lib/date.js | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/date.js b/lib/date.js index 84516de0..76f76f54 100644 --- a/lib/date.js +++ b/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(/^(?[0-9]+)(?hour|day|week|month|year)$/).groups; if (+num === 1) {