DateString.js 1.0 KB

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  5. var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  6. var $isNaN = require('../helpers/isNaN');
  7. var padTimeComponent = require('../helpers/padTimeComponent');
  8. var Type = require('./Type');
  9. var WeekDay = require('./WeekDay');
  10. var MonthFromTime = require('./MonthFromTime');
  11. var YearFromTime = require('./YearFromTime');
  12. var DateFromTime = require('./DateFromTime');
  13. // https://262.ecma-international.org/9.0/#sec-datestring
  14. module.exports = function DateString(tv) {
  15. if (Type(tv) !== 'Number' || $isNaN(tv)) {
  16. throw new $TypeError('Assertion failed: `tv` must be a non-NaN Number');
  17. }
  18. var weekday = weekdays[WeekDay(tv)];
  19. var month = months[MonthFromTime(tv)];
  20. var day = padTimeComponent(DateFromTime(tv));
  21. var year = padTimeComponent(YearFromTime(tv), 4);
  22. return weekday + '\x20' + month + '\x20' + day + '\x20' + year;
  23. };