UNPKG

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