index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import requiredArgs from "../_lib/requiredArgs/index.js";
  2. // Leap year occures every 4 years, except for years that are divisable by 100 and not divisable by 400.
  3. // 1 mean year = (365+1/4-1/100+1/400) days = 365.2425 days
  4. var daysInYear = 365.2425;
  5. /**
  6. * @name milliseconds
  7. * @category Millisecond Helpers
  8. * @summary
  9. * Returns the number of milliseconds in the specified, years, months, weeks, days, hours, minutes and seconds.
  10. *
  11. * @description
  12. * Returns the number of milliseconds in the specified, years, months, weeks, days, hours, minutes and seconds.
  13. *
  14. * One years equals 365.2425 days according to the formula:
  15. *
  16. * > Leap year occures every 4 years, except for years that are divisable by 100 and not divisable by 400.
  17. * > 1 mean year = (365+1/4-1/100+1/400) days = 365.2425 days
  18. *
  19. * One month is a year divided by 12.
  20. *
  21. * @param {Duration} duration - the object with years, months, weeks, days, hours, minutes and seconds to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`.
  22. * @returns {number} the milliseconds
  23. * @throws {TypeError} 1 argument required
  24. *
  25. * @example
  26. * // 1 year in milliseconds
  27. * milliseconds({ years: 1 })
  28. * //=> 31556952000
  29. *
  30. * // 3 months in milliseconds
  31. * milliseconds({ months: 3 })
  32. * //=> 7889238000
  33. */
  34. export default function milliseconds(_ref) {
  35. var years = _ref.years,
  36. months = _ref.months,
  37. weeks = _ref.weeks,
  38. days = _ref.days,
  39. hours = _ref.hours,
  40. minutes = _ref.minutes,
  41. seconds = _ref.seconds;
  42. requiredArgs(1, arguments);
  43. var totalDays = 0;
  44. if (years) totalDays += years * daysInYear;
  45. if (months) totalDays += months * (daysInYear / 12);
  46. if (weeks) totalDays += weeks * 7;
  47. if (days) totalDays += days;
  48. var totalSeconds = totalDays * 24 * 60 * 60;
  49. if (hours) totalSeconds += hours * 60 * 60;
  50. if (minutes) totalSeconds += minutes * 60;
  51. if (seconds) totalSeconds += seconds;
  52. return Math.round(totalSeconds * 1000);
  53. }