isSameMonth.js 1.2 KB

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