getDecade.js 988 B

1234567891011121314151617181920212223242526272829303132
  1. "use strict";
  2. exports.getDecade = getDecade;
  3. var _index = require("./toDate.js");
  4. /**
  5. * @name getDecade
  6. * @category Decade Helpers
  7. * @summary Get the decade of the given date.
  8. *
  9. * @description
  10. * Get the decade of the given date.
  11. *
  12. * @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).
  13. *
  14. * @param date - The given date
  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. function getDecade(date) {
  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 = (0, _index.toDate)(date);
  28. const year = _date.getFullYear();
  29. const decade = Math.floor(year / 10) * 10;
  30. return decade;
  31. }