Added year option.

This commit is contained in:
Mike Cao 2020-07-30 23:08:33 -07:00
parent 38abd673f3
commit 5b45301178
4 changed files with 18 additions and 2 deletions

View file

@ -3,18 +3,22 @@ import {
addMinutes,
addHours,
addDays,
addMonths,
subHours,
subDays,
startOfHour,
startOfDay,
startOfWeek,
startOfMonth,
startOfYear,
endOfHour,
endOfDay,
endOfWeek,
endOfMonth,
endOfYear,
differenceInHours,
differenceInCalendarDays,
differenceInMonths,
} from 'date-fns';
export function getTimezone() {
@ -28,7 +32,7 @@ export function getLocalTime(t) {
export function getDateRange(value) {
const now = new Date();
const { num, unit } = value.match(/^(?<num>[0-9]+)(?<unit>hour|day|week|month)$/).groups;
const { num, unit } = value.match(/^(?<num>[0-9]+)(?<unit>hour|day|week|month|year)$/).groups;
if (+num === 1) {
switch (unit) {
@ -53,6 +57,13 @@ export function getDateRange(value) {
unit: 'day',
value,
};
case 'year':
return {
startDate: startOfYear(now),
endDate: endOfYear(now),
unit: 'month',
value,
};
}
}
@ -77,6 +88,7 @@ export function getDateRange(value) {
const dateFuncs = {
hour: [differenceInHours, addHours, startOfHour],
day: [differenceInCalendarDays, addDays, startOfDay],
month: [differenceInMonths, addMonths, startOfMonth],
};
export function getDateArray(data, startDate, endDate, unit) {