DateString.js 978 B

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