UNPKG

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