MakeDay.js 917 B

123456789101112131415161718192021222324252627282930313233
  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 ToInteger = require('./ToInteger');
  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 = ToInteger(year);
  18. var m = ToInteger(month);
  19. var dt = ToInteger(date);
  20. var ym = y + floor(m / 12);
  21. var mn = modulo(m, 12);
  22. var t = $DateUTC(ym, mn, 1);
  23. if (YearFromTime(t) !== ym || MonthFromTime(t) !== mn || DateFromTime(t) !== 1) {
  24. return NaN;
  25. }
  26. return Day(t) + dt - 1;
  27. };