isSameMonth.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. exports.isSameMonth = isSameMonth;
  3. var _index = require("./toDate.js");
  4. /**
  5. * @name isSameMonth
  6. * @category Month Helpers
  7. * @summary Are the given dates in the same month (and year)?
  8. *
  9. * @description
  10. * Are the given dates in the same month (and year)?
  11. *
  12. * @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).
  13. *
  14. * @param dateLeft - The first date to check
  15. * @param dateRight - The second date to check
  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. function isSameMonth(dateLeft, dateRight) {
  30. const _dateLeft = (0, _index.toDate)(dateLeft);
  31. const _dateRight = (0, _index.toDate)(dateRight);
  32. return (
  33. _dateLeft.getFullYear() === _dateRight.getFullYear() &&
  34. _dateLeft.getMonth() === _dateRight.getMonth()
  35. );
  36. }