getWeeksInMonth.cjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. exports.getWeeksInMonth = getWeeksInMonth;
  3. var _index = require("./differenceInCalendarWeeks.cjs");
  4. var _index2 = require("./lastDayOfMonth.cjs");
  5. var _index3 = require("./startOfMonth.cjs");
  6. var _index4 = require("./toDate.cjs");
  7. /**
  8. * The {@link getWeeksInMonth} function options.
  9. */
  10. /**
  11. * @name getWeeksInMonth
  12. * @category Week Helpers
  13. * @summary Get the number of calendar weeks a month spans.
  14. *
  15. * @description
  16. * Get the number of calendar weeks the month in the given date spans.
  17. *
  18. * @param date - The given date
  19. * @param options - An object with options.
  20. *
  21. * @returns The number of calendar weeks
  22. *
  23. * @example
  24. * // How many calendar weeks does February 2015 span?
  25. * const result = getWeeksInMonth(new Date(2015, 1, 8))
  26. * //=> 4
  27. *
  28. * @example
  29. * // If the week starts on Monday,
  30. * // how many calendar weeks does July 2017 span?
  31. * const result = getWeeksInMonth(new Date(2017, 6, 5), { weekStartsOn: 1 })
  32. * //=> 6
  33. */
  34. function getWeeksInMonth(date, options) {
  35. const contextDate = (0, _index4.toDate)(date, options?.in);
  36. return (
  37. (0, _index.differenceInCalendarWeeks)(
  38. (0, _index2.lastDayOfMonth)(contextDate, options),
  39. (0, _index3.startOfMonth)(contextDate, options),
  40. options,
  41. ) + 1
  42. );
  43. }