milliseconds.cjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. exports.milliseconds = milliseconds;
  3. var _index = require("./constants.cjs");
  4. /**
  5. * @name milliseconds
  6. * @category Millisecond Helpers
  7. * @summary
  8. * Returns the number of milliseconds in the specified, years, months, weeks, days, hours, minutes and seconds.
  9. *
  10. * @description
  11. * Returns the number of milliseconds in the specified, years, months, weeks, days, hours, minutes and seconds.
  12. *
  13. * One years equals 365.2425 days according to the formula:
  14. *
  15. * > Leap year occurs every 4 years, except for years that are divisible by 100 and not divisible by 400.
  16. * > 1 mean year = (365+1/4-1/100+1/400) days = 365.2425 days
  17. *
  18. * One month is a year divided by 12.
  19. *
  20. * @param duration - The object with years, months, weeks, days, hours, minutes and seconds to be added.
  21. *
  22. * @returns The milliseconds
  23. *
  24. * @example
  25. * // 1 year in milliseconds
  26. * milliseconds({ years: 1 })
  27. * //=> 31556952000
  28. *
  29. * // 3 months in milliseconds
  30. * milliseconds({ months: 3 })
  31. * //=> 7889238000
  32. */
  33. function milliseconds({ years, months, weeks, days, hours, minutes, seconds }) {
  34. let totalDays = 0;
  35. if (years) totalDays += years * _index.daysInYear;
  36. if (months) totalDays += months * (_index.daysInYear / 12);
  37. if (weeks) totalDays += weeks * 7;
  38. if (days) totalDays += days;
  39. let totalSeconds = totalDays * 24 * 60 * 60;
  40. if (hours) totalSeconds += hours * 60 * 60;
  41. if (minutes) totalSeconds += minutes * 60;
  42. if (seconds) totalSeconds += seconds;
  43. return Math.trunc(totalSeconds * 1000);
  44. }