isSameMonth.mjs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { toDate } from "./toDate.mjs";
  2. /**
  3. * @name isSameMonth
  4. * @category Month Helpers
  5. * @summary Are the given dates in the same month (and year)?
  6. *
  7. * @description
  8. * Are the given dates in the same month (and year)?
  9. *
  10. * @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).
  11. *
  12. * @param dateLeft - The first date to check
  13. * @param dateRight - The second date to check
  14. *
  15. * @returns The dates are in the same month (and year)
  16. *
  17. * @example
  18. * // Are 2 September 2014 and 25 September 2014 in the same month?
  19. * const result = isSameMonth(new Date(2014, 8, 2), new Date(2014, 8, 25))
  20. * //=> true
  21. *
  22. * @example
  23. * // Are 2 September 2014 and 25 September 2015 in the same month?
  24. * const result = isSameMonth(new Date(2014, 8, 2), new Date(2015, 8, 25))
  25. * //=> false
  26. */
  27. export function isSameMonth(dateLeft, dateRight) {
  28. const _dateLeft = toDate(dateLeft);
  29. const _dateRight = toDate(dateRight);
  30. return (
  31. _dateLeft.getFullYear() === _dateRight.getFullYear() &&
  32. _dateLeft.getMonth() === _dateRight.getMonth()
  33. );
  34. }
  35. // Fallback for modularized imports:
  36. export default isSameMonth;