Alternatives to a dedicated lastDayOfWeek function in date-fns
- Import necessary functions
import { endOfWeek, getDay } from 'date-fns';
- Use endOfWeek and getDay
const date = new Date(); // Replace with your desired date
// Get the start of the current week
const startOfWeek = endOfWeek(date, { weekStartsOn: Sunday }); // Adjust week start if needed
// Get the day of the week (0 for Sunday, 6 for Saturday)
const dayOfWeek = getDay(startOfWeek);
// Calculate the offset for the last day
const lastDayOffset = (dayOfWeek === 0) ? 6 : dayOfWeek - 1;
// Add the offset to the start of the week to get the last day
const lastDayOfWeek = addDays(startOfWeek, lastDayOffset); // Use addDays from date-fns (not included here)
- Unfortunately, date-fns doesn't have a built-in
addDays
function. You'd need to import it from date-fns or use a different library/approach to add the offset days to thestartOfWeek
to get the finallastDayOfWeek
. - We calculate the offset based on the retrieved day. If it's Sunday (dayOfWeek === 0), the last day would be 6 days ahead (offset of 6). Otherwise, the offset is the current day minus 1.
getDay
retrieves the day of the week (0-6) for the given date.endOfWeek
takes a date and an optional configuration object. By default, it considers Sunday as the first day of the week. You can adjust this with theweekStartsOn
property.
Alternative approach
You can also achieve this by simply subtracting one day from the result of endOfWeek
:
const lastDayOfWeek = subDays(endOfWeek(date), 1); // Use subDays from date-fns (not included here)
Example 1: Finding the last day of the current week (Sunday as week start)
import { endOfWeek, getDay, subDays } from 'date-fns';
const today = new Date();
// Option 1: Using subDays
const lastDayOfWeek = subDays(endOfWeek(today), 1);
console.log("Last day of the current week (using subDays):", lastDayOfWeek);
// Option 2: Calculating the offset
const dayOfWeek = getDay(endOfWeek(today));
const lastDayOffset = (dayOfWeek === 0) ? 6 : dayOfWeek - 1;
const lastDayOfWeekWithOffset = addDays(endOfWeek(today), lastDayOffset); // Replace addDays with your implementation
console.log("Last day of the current week (calculating offset):", lastDayOfWeekWithOffset);
This example demonstrates both approaches mentioned earlier. The first option uses subDays
for a simpler approach assuming Sunday as the week start. The second option calculates the offset based on the current day to find the last day.
Example 2: Finding the last day of the week for a specific date (considering Monday as week start)
import { endOfWeek, getDay } from 'date-fns';
const specificDate = new Date(2024, 6, 19); // Adjust year, month, and day as needed
const weekStartOnMonday = { weekStartsOn: 1 };
const endOfWeekForSpecificDate = endOfWeek(specificDate, weekStartOnMonday);
const dayOfWeekForSpecificDate = getDay(endOfWeekForSpecificDate);
const lastDayOffset = (dayOfWeekForSpecificDate === 0) ? 6 : dayOfWeekForSpecificDate - 1;
const lastDayOfWeekForSpecificDate = addDays(endOfWeekForSpecificDate, lastDayOffset); // Replace addDays with your implementation
console.log("Last day of the week for", specificDate, "considering Monday as week start:", lastDayOfWeekForSpecificDate);
This example finds the last day of the week for a specific date, considering Monday as the week start. It adjusts the endOfWeek
function with the weekStartsOn
option and calculates the offset based on the retrieved day.
- Using subDays
This is the simplest approach if you already have the endOfWeek
functionality:
import { endOfWeek, subDays } from 'date-fns';
const lastDayOfWeek = subDays(endOfWeek(date), 1);
- Calculating the Offset
This approach offers more flexibility if you need to handle different week starting days:
import { endOfWeek, getDay } from 'date-fns';
const dayOfWeek = getDay(endOfWeek(date));
const lastDayOffset = (dayOfWeek === 0) ? 6 : dayOfWeek - 1;
const lastDayOfWeek = addDays(endOfWeek(date), lastDayOffset); // Replace addDays with your implementation
- Native Javascript Methods (Limited Functionality)
For basic scenarios, you can leverage native Javascript methods:
- date.setDate(date.getDate() - 1)
This directly subtracts one day from the current date object. However, it doesn't account for week boundaries.
- Third-party Libraries
Other libraries like Moment.js or Luxon have dedicated functions for finding the last day of the week:
- Luxon
DateTime.local().endOf('week').minus({ days: 1 })
- Moment.js
moment().endOf('week').subtract(1, 'days')
- If you're already using Moment.js or Luxon, their dedicated functions provide a convenient solution.
- For very basic scenarios, the native Javascript method could work.
- If you need to handle different week starting days or prefer more control over the calculation, the offset approach is better.
- If simplicity and Sunday as the week start are your requirements, using
subDays
is ideal.