getDaysInMonth.js 1.0 KB

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