[MONGOOSE] current week & current month (destruct mode) - fourslickz/notes GitHub Wiki
const getCurrentWeek = () => {
const today = new Date();
// Get the current day of the week (0 is Sunday, 1 is Monday, etc.)
const dayOfWeek = today.getDay();
// Calculate how many days to subtract to get to Monday
const diffToMonday = dayOfWeek === 0 ? 6 : dayOfWeek - 1;
// Start of the week (Monday)
const startOfWeek = new Date(today);
startOfWeek.setDate(today.getDate() - diffToMonday);
startOfWeek.setHours(0, 0, 0, 0);
// End of the week (Sunday)
const endOfWeek = new Date(startOfWeek);
endOfWeek.setDate(startOfWeek.getDate() + 6);
endOfWeek.setHours(23, 59, 59, 999);
return { startOfWeek, endOfWeek };
};
const getCurrentMonth = () => {
const startOfMonth = new Date();
startOfMonth.setDate(1); // Set the date to the first day of the month
startOfMonth.setHours(0, 0, 0, 0); // Set time to the start of the day
const endOfMonth = new Date(startOfMonth);
endOfMonth.setMonth(endOfMonth.getMonth() + 1); // Move to the next month
endOfMonth.setDate(0); // Set to the last day of the current month
endOfMonth.setHours(23, 59, 59, 999); // Set time to the end of the day
return { startOfMonth, endOfMonth };
};
const { startOfWeek, endOfWeek } = getCurrentWeek();
const { startOfMonth, endOfMonth } = getCurrentMonth();