lastDayOfWeek.cjs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. exports.lastDayOfWeek = lastDayOfWeek;
  3. var _index = require("./_lib/defaultOptions.cjs");
  4. var _index2 = require("./toDate.cjs");
  5. /**
  6. * The {@link lastDayOfWeek} function options.
  7. */
  8. /**
  9. * @name lastDayOfWeek
  10. * @category Week Helpers
  11. * @summary Return the last day of a week for the given date.
  12. *
  13. * @description
  14. * Return the last day of a week for the given date.
  15. * The result will be in the local timezone unless a context is specified.
  16. *
  17. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  18. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  19. *
  20. * @param date - The original date
  21. * @param options - An object with options
  22. *
  23. * @returns The last day of a week
  24. */
  25. function lastDayOfWeek(date, options) {
  26. const defaultOptions = (0, _index.getDefaultOptions)();
  27. const weekStartsOn =
  28. options?.weekStartsOn ??
  29. options?.locale?.options?.weekStartsOn ??
  30. defaultOptions.weekStartsOn ??
  31. defaultOptions.locale?.options?.weekStartsOn ??
  32. 0;
  33. const _date = (0, _index2.toDate)(date, options?.in);
  34. const day = _date.getDay();
  35. const diff = (day < weekStartsOn ? -7 : 0) + 6 - (day - weekStartsOn);
  36. _date.setHours(0, 0, 0, 0);
  37. _date.setDate(_date.getDate() + diff);
  38. return _date;
  39. }