Get interval dates #3005
Unanswered
frangoni
asked this question in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello everyone! I coded an implementation to get start date and end date of common intervals. Hope I can help someone with this.
It supports the following intervals: today, yesterday, last7Days, thisWeek. lastWeek, last30Days, thisMonth, lastMonth, yearToDate, lifetime
`import { endOfMonth, endOfWeek, startOfMonth, startOfWeek, sub } from 'date-fns';
const map: {
[key: string]: {
startDate: Date;
endDate: Date;
};
} = {
today: {
startDate: new Date(),
endDate: new Date(),
},
yesterday: {
startDate: sub(new Date(), {
days: 1,
}),
endDate: sub(new Date(), {
days: 1,
}),
},
last7Days: {
startDate: sub(new Date(), {
days: 7,
}),
endDate: new Date(),
},
thisWeek: {
startDate: startOfWeek(new Date()),
endDate: new Date(),
},
lastWeek: {
startDate: startOfWeek(
sub(new Date(), {
days: 7,
})
),
endDate: endOfWeek(
sub(new Date(), {
days: 7,
})
),
},
last30Days: {
startDate: sub(new Date(), {
days: 30,
}),
endDate: new Date(),
},
thisMonth: { startDate: startOfMonth(new Date()), endDate: new Date() },
lastMonth: {
startDate: startOfMonth(
sub(new Date(), {
months: 1,
})
),
endDate: endOfMonth(
sub(new Date(), {
months: 1,
})
),
},
yearToDate: {
startDate: sub(new Date(), {
years: 1,
}),
endDate: new Date(),
},
lifetime: {
startDate: new Date(0),
endDate: new Date(),
},
};
export function getDatesInterval(sentence: string) {
let { endDate, startDate } = map[sentence];
return { startDate, endDate };
}`
Beta Was this translation helpful? Give feedback.
All reactions