isSameWeek.js 1.5 KB

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