getDecade.cjs 922 B

1234567891011121314151617181920212223242526272829303132333435
  1. "use strict";
  2. exports.getDecade = getDecade;
  3. var _index = require("./toDate.cjs");
  4. /**
  5. * The {@link getDecade} function options.
  6. */
  7. /**
  8. * @name getDecade
  9. * @category Decade Helpers
  10. * @summary Get the decade of the given date.
  11. *
  12. * @description
  13. * Get the decade of the given date.
  14. *
  15. * @param date - The given date
  16. * @param options - An object with options
  17. *
  18. * @returns The year of decade
  19. *
  20. * @example
  21. * // Which decade belongs 27 November 1942?
  22. * const result = getDecade(new Date(1942, 10, 27))
  23. * //=> 1940
  24. */
  25. function getDecade(date, options) {
  26. // TODO: Switch to more technical definition in of decades that start with 1
  27. // end with 0. I.e. 2001-2010 instead of current 2000-2009. It's a breaking
  28. // change, so it can only be done in 4.0.
  29. const _date = (0, _index.toDate)(date, options?.in);
  30. const year = _date.getFullYear();
  31. const decade = Math.floor(year / 10) * 10;
  32. return decade;
  33. }