1 /* This helper is inspired from https://github.com/date-fns/date-fns/blob/main/src/isSameWeek/index.ts helper */
2 import { startOfWeek, subDays } from 'date-fns';
5 * The {@link isLastWeek} function options.
10 * @category Week Helpers
11 * @summary Is the given date in the last week?
14 * Is the given date in the last week?
16 * @param dirtyDate - the date to check
17 * @param options - an object with options.
18 * @returns the date is in the last week
21 * // If Current date is 03/27/2023
22 * // Is 20 March 2023 in the last week?
23 * const result = isLastWeek(new Date(2023, 2, 20))
27 * // If Current date is 03/27/2023 and if week starts with Monday,
28 * // Is 20 March 2023 in the last week?
29 * const result = isLastWeek(new Date(2023, 2, 20), {
35 * // If Current date is 03/27/2023
36 * // Is 27 March 2023 in the last week?
37 * const result = isLastWeek(new Date(2023, 2, 27))
41 export default function isLastWeek(
42 dirtyDate: Date | number,
43 options?: { locale?: Locale; weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 }
45 const currentDate = new Date();
46 const dateStartOfWeek = startOfWeek(currentDate, options);
48 const lastWeekStart = subDays(dateStartOfWeek, 7);
49 const lastWeekEnd = subDays(lastWeekStart, -6);
51 const dateToCheck = new Date(dirtyDate);
52 return dateToCheck >= lastWeekStart && dateToCheck <= lastWeekEnd;