index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = endOfWeek;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  8. var _index3 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /**
  11. * @name endOfWeek
  12. * @category Week Helpers
  13. * @summary Return the end of a week for the given date.
  14. *
  15. * @description
  16. * Return the end of a week for the given date.
  17. * The result will be in the local timezone.
  18. *
  19. * ### v2.0.0 breaking changes:
  20. *
  21. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  22. *
  23. * @param {Date|Number} date - the original date
  24. * @param {Object} [options] - an object with options.
  25. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  26. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  27. * @returns {Date} the end of a week
  28. * @throws {TypeError} 1 argument required
  29. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  30. *
  31. * @example
  32. * // The end of a week for 2 September 2014 11:55:00:
  33. * const result = endOfWeek(new Date(2014, 8, 2, 11, 55, 0))
  34. * //=> Sat Sep 06 2014 23:59:59.999
  35. *
  36. * @example
  37. * // If the week starts on Monday, the end of the week for 2 September 2014 11:55:00:
  38. * const result = endOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 })
  39. * //=> Sun Sep 07 2014 23:59:59.999
  40. */
  41. function endOfWeek(dirtyDate, dirtyOptions) {
  42. (0, _index3.default)(1, arguments);
  43. var options = dirtyOptions || {};
  44. var locale = options.locale;
  45. var localeWeekStartsOn = locale && locale.options && locale.options.weekStartsOn;
  46. var defaultWeekStartsOn = localeWeekStartsOn == null ? 0 : (0, _index2.default)(localeWeekStartsOn);
  47. var weekStartsOn = options.weekStartsOn == null ? defaultWeekStartsOn : (0, _index2.default)(options.weekStartsOn); // Test if weekStartsOn is between 0 and 6 _and_ is not NaN
  48. if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
  49. throw new RangeError('weekStartsOn must be between 0 and 6 inclusively');
  50. }
  51. var date = (0, _index.default)(dirtyDate);
  52. var day = date.getDay();
  53. var diff = (day < weekStartsOn ? -7 : 0) + 6 - (day - weekStartsOn);
  54. date.setDate(date.getDate() + diff);
  55. date.setHours(23, 59, 59, 999);
  56. return date;
  57. }
  58. module.exports = exports.default;