UNPKG

1.22 kBJavaScriptView Raw
1import { normalizeDates } from "./_lib/normalizeDates.js";
2
3/**
4 * The {@link isSameMonth} function options.
5 */
6
7/**
8 * @name isSameMonth
9 * @category Month Helpers
10 * @summary Are the given dates in the same month (and year)?
11 *
12 * @description
13 * Are the given dates in the same month (and year)?
14 *
15 * @param laterDate - The first date to check
16 * @param earlierDate - The second date to check
17 * @param options - An object with options
18 *
19 * @returns The dates are in the same month (and year)
20 *
21 * @example
22 * // Are 2 September 2014 and 25 September 2014 in the same month?
23 * const result = isSameMonth(new Date(2014, 8, 2), new Date(2014, 8, 25))
24 * //=> true
25 *
26 * @example
27 * // Are 2 September 2014 and 25 September 2015 in the same month?
28 * const result = isSameMonth(new Date(2014, 8, 2), new Date(2015, 8, 25))
29 * //=> false
30 */
31export function isSameMonth(laterDate, earlierDate, options) {
32 const [laterDate_, earlierDate_] = normalizeDates(
33 options?.in,
34 laterDate,
35 earlierDate,
36 );
37 return (
38 laterDate_.getFullYear() === earlierDate_.getFullYear() &&
39 laterDate_.getMonth() === earlierDate_.getMonth()
40 );
41}
42
43// Fallback for modularized imports:
44export default isSameMonth;