MakeDay.js 1007 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $DateUTC = GetIntrinsic('%Date.UTC%');
  4. var $isFinite = require('../helpers/isFinite');
  5. var DateFromTime = require('./DateFromTime');
  6. var Day = require('./Day');
  7. var floor = require('./floor');
  8. var modulo = require('./modulo');
  9. var MonthFromTime = require('./MonthFromTime');
  10. var ToIntegerOrInfinity = require('./ToIntegerOrInfinity');
  11. var YearFromTime = require('./YearFromTime');
  12. // https://262.ecma-international.org/5.1/#sec-15.9.1.12
  13. module.exports = function MakeDay(year, month, date) {
  14. if (!$isFinite(year) || !$isFinite(month) || !$isFinite(date)) {
  15. return NaN;
  16. }
  17. var y = ToIntegerOrInfinity(year);
  18. var m = ToIntegerOrInfinity(month);
  19. var dt = ToIntegerOrInfinity(date);
  20. var ym = y + floor(m / 12);
  21. if (!$isFinite(ym)) {
  22. return NaN;
  23. }
  24. var mn = modulo(m, 12);
  25. var t = $DateUTC(ym, mn, 1);
  26. if (YearFromTime(t) !== ym || MonthFromTime(t) !== mn || DateFromTime(t) !== 1) {
  27. return NaN;
  28. }
  29. return Day(t) + dt - 1;
  30. };