The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
export const getFirstAndLastDay = (day: number) => {
const today = new Date();
const currentDay = today.getDate();
const currentMonth = today.getMonth();
const currentYear = today.getFullYear();
if (currentDay >= day) {
// if the current day is greater than target day, it means that we just passed it
return {
firstDay: new Date(currentYear, currentMonth, day),
lastDay: new Date(currentYear, currentMonth + 1, day - 1),
};
} else {
// if the current day is less than target day, it means that we haven't passed it yet
const lastYear = currentMonth === 0 ? currentYear - 1 : currentYear; // if the current month is January, we need to go back a year
const lastMonth = currentMonth === 0 ? 11 : currentMonth - 1; // if the current month is January, we need to go back to December
return {
firstDay: new Date(lastYear, lastMonth, day),
lastDay: new Date(currentYear, currentMonth, day - 1),
};
}
};