index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = endOfDecade;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * @name endOfDecade
  11. * @category Decade Helpers
  12. * @summary Return the end of a decade for the given date.
  13. *
  14. * @description
  15. * Return the end of a decade for the given date.
  16. *
  17. * ### v2.0.0 breaking changes:
  18. *
  19. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  20. *
  21. * @param {Date|Number} date - the original date
  22. * @returns {Date} the end of a decade
  23. * @param {Object} [options] - an object with options.
  24. * @param {0|1|2} [options.additionalDigits=2] - passed to `toDate`. See [toDate]{@link https://date-fns.org/docs/toDate}
  25. * @throws {TypeError} 1 argument required
  26. * @throws {RangeError} `options.additionalDigits` must be 0, 1 or 2
  27. *
  28. * @example
  29. * // The end of a decade for 12 May 1984 00:00:00:
  30. * const result = endOfDecade(new Date(1984, 4, 12, 00, 00, 00))
  31. * //=> Dec 31 1989 23:59:59.999
  32. */
  33. function endOfDecade(dirtyDate) {
  34. (0, _index2.default)(1, arguments);
  35. var date = (0, _index.default)(dirtyDate);
  36. var year = date.getFullYear();
  37. var decade = 9 + Math.floor(year / 10) * 10;
  38. date.setFullYear(decade, 11, 31);
  39. date.setHours(23, 59, 59, 999);
  40. return date;
  41. }
  42. module.exports = exports.default;