index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var startOfWeek = require('../start_of_week/index.js')
  2. /**
  3. * @category Week Helpers
  4. * @summary Are the given dates in the same week?
  5. *
  6. * @description
  7. * Are the given dates in the same week?
  8. *
  9. * @param {Date|String|Number} dateLeft - the first date to check
  10. * @param {Date|String|Number} dateRight - the second date to check
  11. * @param {Object} [options] - the object with options
  12. * @param {Number} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  13. * @returns {Boolean} the dates are in the same week
  14. *
  15. * @example
  16. * // Are 31 August 2014 and 4 September 2014 in the same week?
  17. * var result = isSameWeek(
  18. * new Date(2014, 7, 31),
  19. * new Date(2014, 8, 4)
  20. * )
  21. * //=> true
  22. *
  23. * @example
  24. * // If week starts with Monday,
  25. * // are 31 August 2014 and 4 September 2014 in the same week?
  26. * var result = isSameWeek(
  27. * new Date(2014, 7, 31),
  28. * new Date(2014, 8, 4),
  29. * {weekStartsOn: 1}
  30. * )
  31. * //=> false
  32. */
  33. function isSameWeek (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
  34. var dateLeftStartOfWeek = startOfWeek(dirtyDateLeft, dirtyOptions)
  35. var dateRightStartOfWeek = startOfWeek(dirtyDateRight, dirtyOptions)
  36. return dateLeftStartOfWeek.getTime() === dateRightStartOfWeek.getTime()
  37. }
  38. module.exports = isSameWeek