getDaysInMonth.cjs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. exports.getDaysInMonth = getDaysInMonth;
  3. var _index = require("./constructFrom.cjs");
  4. var _index2 = require("./toDate.cjs");
  5. /**
  6. * The {@link getDaysInMonth} function options.
  7. */
  8. /**
  9. * @name getDaysInMonth
  10. * @category Month Helpers
  11. * @summary Get the number of days in a month of the given date.
  12. *
  13. * @description
  14. * Get the number of days in a month of the given date, considering the context if provided.
  15. *
  16. * @param date - The given date
  17. * @param options - An object with options
  18. *
  19. * @returns The number of days in a month
  20. *
  21. * @example
  22. * // How many days are in February 2000?
  23. * const result = getDaysInMonth(new Date(2000, 1))
  24. * //=> 29
  25. */
  26. function getDaysInMonth(date, options) {
  27. const _date = (0, _index2.toDate)(date, options?.in);
  28. const year = _date.getFullYear();
  29. const monthIndex = _date.getMonth();
  30. const lastDayOfMonth = (0, _index.constructFrom)(_date, 0);
  31. lastDayOfMonth.setFullYear(year, monthIndex + 1, 0);
  32. lastDayOfMonth.setHours(0, 0, 0, 0);
  33. return lastDayOfMonth.getDate();
  34. }