getDecade.mjs 1004 B

123456789101112131415161718192021222324252627282930313233
  1. import { toDate } from "./toDate.mjs";
  2. /**
  3. * @name getDecade
  4. * @category Decade Helpers
  5. * @summary Get the decade of the given date.
  6. *
  7. * @description
  8. * Get the decade of the given date.
  9. *
  10. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  11. *
  12. * @param date - The given date
  13. *
  14. * @returns The year of decade
  15. *
  16. * @example
  17. * // Which decade belongs 27 November 1942?
  18. * const result = getDecade(new Date(1942, 10, 27))
  19. * //=> 1940
  20. */
  21. export function getDecade(date) {
  22. // TODO: Switch to more technical definition in of decades that start with 1
  23. // end with 0. I.e. 2001-2010 instead of current 2000-2009. It's a breaking
  24. // change, so it can only be done in 4.0.
  25. const _date = toDate(date);
  26. const year = _date.getFullYear();
  27. const decade = Math.floor(year / 10) * 10;
  28. return decade;
  29. }
  30. // Fallback for modularized imports:
  31. export default getDecade;