index.js 773 B

12345678910111213141516171819202122232425262728
  1. var parse = require('../parse/index.js')
  2. /**
  3. * @category Month Helpers
  4. * @summary Get the number of days in a month of the given date.
  5. *
  6. * @description
  7. * Get the number of days in a month of the given date.
  8. *
  9. * @param {Date|String|Number} date - the given date
  10. * @returns {Number} the number of days in a month
  11. *
  12. * @example
  13. * // How many days are in February 2000?
  14. * var result = getDaysInMonth(new Date(2000, 1))
  15. * //=> 29
  16. */
  17. function getDaysInMonth (dirtyDate) {
  18. var date = parse(dirtyDate)
  19. var year = date.getFullYear()
  20. var monthIndex = date.getMonth()
  21. var lastDayOfMonth = new Date(0)
  22. lastDayOfMonth.setFullYear(year, monthIndex + 1, 0)
  23. lastDayOfMonth.setHours(0, 0, 0, 0)
  24. return lastDayOfMonth.getDate()
  25. }
  26. module.exports = getDaysInMonth