getDecade.js 936 B

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