index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = milliseconds;
  6. var _index = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. // Leap year occures every 4 years, except for years that are divisable by 100 and not divisable by 400.
  9. // 1 mean year = (365+1/4-1/100+1/400) days = 365.2425 days
  10. var yearInDays = 365.2425;
  11. /**
  12. * @name milliseconds
  13. * @category Millisecond Helpers
  14. * @summary
  15. * Returns the number of milliseconds in the specified, years, months, weeks, days, hours, minutes and seconds.
  16. *
  17. * @description
  18. * Returns the number of milliseconds in the specified, years, months, weeks, days, hours, minutes and seconds.
  19. *
  20. * One years equals 365.2425 days according to the formula:
  21. *
  22. * > Leap year occures every 4 years, except for years that are divisable by 100 and not divisable by 400.
  23. * > 1 mean year = (365+1/4-1/100+1/400) days = 365.2425 days
  24. *
  25. * One month is a year divided by 12.
  26. *
  27. * @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`.
  28. * @returns {number} the milliseconds
  29. * @throws {TypeError} 1 argument required
  30. *
  31. * @example
  32. * // 1 year in milliseconds
  33. * milliseconds({ years: 1 })
  34. * //=> 31556952000
  35. *
  36. * // 3 months in milliseconds
  37. * milliseconds({ months: 3 })
  38. * //=> 7889238000
  39. */
  40. function milliseconds(_ref) {
  41. var years = _ref.years,
  42. months = _ref.months,
  43. weeks = _ref.weeks,
  44. days = _ref.days,
  45. hours = _ref.hours,
  46. minutes = _ref.minutes,
  47. seconds = _ref.seconds;
  48. (0, _index.default)(1, arguments);
  49. var totalDays = 0;
  50. if (years) totalDays += years * yearInDays;
  51. if (months) totalDays += months * (yearInDays / 12);
  52. if (weeks) totalDays += weeks * 7;
  53. if (days) totalDays += days;
  54. var totalSeconds = totalDays * 24 * 60 * 60;
  55. if (hours) totalSeconds += hours * 60 * 60;
  56. if (minutes) totalSeconds += minutes * 60;
  57. if (seconds) totalSeconds += seconds;
  58. return Math.round(totalSeconds * 1000);
  59. }
  60. module.exports = exports.default;