monthsToYears.js 699 B

123456789101112131415161718192021222324252627282930
  1. import { monthsInYear } from "./constants.js";
  2. /**
  3. * @name monthsToYears
  4. * @category Conversion Helpers
  5. * @summary Convert number of months to years.
  6. *
  7. * @description
  8. * Convert a number of months to a full number of years.
  9. *
  10. * @param months - The number of months to be converted
  11. *
  12. * @returns The number of months converted in years
  13. *
  14. * @example
  15. * // Convert 36 months to years:
  16. * const result = monthsToYears(36)
  17. * //=> 3
  18. *
  19. * // It uses floor rounding:
  20. * const result = monthsToYears(40)
  21. * //=> 3
  22. */
  23. export function monthsToYears(months) {
  24. const years = months / monthsInYear;
  25. return Math.trunc(years);
  26. }
  27. // Fallback for modularized imports:
  28. export default monthsToYears;