index.js 895 B

12345678910111213141516171819202122232425262728293031
  1. import toDate from "../toDate/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. /**
  4. * @name getDecade
  5. * @category Decade Helpers
  6. * @summary Get the decade of the given date.
  7. *
  8. * @description
  9. * Get the decade of 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 given date
  16. * @returns {Number} the year of decade
  17. * @throws {TypeError} 1 argument required
  18. *
  19. * @example
  20. * // Which decade belongs 27 November 1942?
  21. * const result = getDecade(new Date(1942, 10, 27))
  22. * //=> 1940
  23. */
  24. export default function getDecade(dirtyDate) {
  25. requiredArgs(1, arguments);
  26. var date = toDate(dirtyDate);
  27. var year = date.getFullYear();
  28. var decade = Math.floor(year / 10) * 10;
  29. return decade;
  30. }