index.js 910 B

1234567891011121314151617181920212223242526272829
  1. import toDate from "../toDate/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. /**
  4. * @name getDaysInMonth
  5. * @category Month Helpers
  6. * @summary Get the number of days in a month of the given date.
  7. *
  8. * @description
  9. * Get the number of days in a month of the given date.
  10. *
  11. * @param {Date|Number} date - the given date
  12. * @returns {Number} the number of days in a month
  13. * @throws {TypeError} 1 argument required
  14. *
  15. * @example
  16. * // How many days are in February 2000?
  17. * const result = getDaysInMonth(new Date(2000, 1))
  18. * //=> 29
  19. */
  20. export default function getDaysInMonth(dirtyDate) {
  21. requiredArgs(1, arguments);
  22. var date = toDate(dirtyDate);
  23. var year = date.getFullYear();
  24. var monthIndex = date.getMonth();
  25. var lastDayOfMonth = new Date(0);
  26. lastDayOfMonth.setFullYear(year, monthIndex + 1, 0);
  27. lastDayOfMonth.setHours(0, 0, 0, 0);
  28. return lastDayOfMonth.getDate();
  29. }