index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getDaysInMonth;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * @name getDaysInMonth
  11. * @category Month Helpers
  12. * @summary Get the number of days in a month of the given date.
  13. *
  14. * @description
  15. * Get the number of days in a month of the given date.
  16. *
  17. * ### v2.0.0 breaking changes:
  18. *
  19. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  20. *
  21. * @param {Date|Number} date - the given date
  22. * @returns {Number} the number of days in a month
  23. * @throws {TypeError} 1 argument required
  24. *
  25. * @example
  26. * // How many days are in February 2000?
  27. * const result = getDaysInMonth(new Date(2000, 1))
  28. * //=> 29
  29. */
  30. function getDaysInMonth(dirtyDate) {
  31. (0, _index2.default)(1, arguments);
  32. var date = (0, _index.default)(dirtyDate);
  33. var year = date.getFullYear();
  34. var monthIndex = date.getMonth();
  35. var lastDayOfMonth = new Date(0);
  36. lastDayOfMonth.setFullYear(year, monthIndex + 1, 0);
  37. lastDayOfMonth.setHours(0, 0, 0, 0);
  38. return lastDayOfMonth.getDate();
  39. }
  40. module.exports = exports.default;