isSameWeek.cjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. exports.isSameWeek = isSameWeek;
  3. var _index = require("./_lib/normalizeDates.cjs");
  4. var _index2 = require("./startOfWeek.cjs");
  5. /**
  6. * The {@link isSameWeek} function options.
  7. */
  8. /**
  9. * @name isSameWeek
  10. * @category Week Helpers
  11. * @summary Are the given dates in the same week (and month and year)?
  12. *
  13. * @description
  14. * Are the given dates in the same week (and month and year)?
  15. *
  16. * @param laterDate - The first date to check
  17. * @param earlierDate - The second date to check
  18. * @param options - An object with options
  19. *
  20. * @returns The dates are in the same week (and month and year)
  21. *
  22. * @example
  23. * // Are 31 August 2014 and 4 September 2014 in the same week?
  24. * const result = isSameWeek(new Date(2014, 7, 31), new Date(2014, 8, 4))
  25. * //=> true
  26. *
  27. * @example
  28. * // If week starts with Monday,
  29. * // are 31 August 2014 and 4 September 2014 in the same week?
  30. * const result = isSameWeek(new Date(2014, 7, 31), new Date(2014, 8, 4), {
  31. * weekStartsOn: 1
  32. * })
  33. * //=> false
  34. *
  35. * @example
  36. * // Are 1 January 2014 and 1 January 2015 in the same week?
  37. * const result = isSameWeek(new Date(2014, 0, 1), new Date(2015, 0, 1))
  38. * //=> false
  39. */
  40. function isSameWeek(laterDate, earlierDate, options) {
  41. const [laterDate_, earlierDate_] = (0, _index.normalizeDates)(
  42. options?.in,
  43. laterDate,
  44. earlierDate,
  45. );
  46. return (
  47. +(0, _index2.startOfWeek)(laterDate_, options) ===
  48. +(0, _index2.startOfWeek)(earlierDate_, options)
  49. );
  50. }