index.js 1.3 KB

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