_date-to-iso-string.js 996 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. // 20.3.4.36 / 15.9.5.43 Date.prototype.toISOString()
  3. var fails = require('./_fails');
  4. var getTime = Date.prototype.getTime;
  5. var $toISOString = Date.prototype.toISOString;
  6. var lz = function (num) {
  7. return num > 9 ? num : '0' + num;
  8. };
  9. // PhantomJS / old WebKit has a broken implementations
  10. module.exports = (fails(function () {
  11. return $toISOString.call(new Date(-5e13 - 1)) != '0385-07-25T07:06:39.999Z';
  12. }) || !fails(function () {
  13. $toISOString.call(new Date(NaN));
  14. })) ? function toISOString() {
  15. if (!isFinite(getTime.call(this))) throw RangeError('Invalid time value');
  16. var d = this;
  17. var y = d.getUTCFullYear();
  18. var m = d.getUTCMilliseconds();
  19. var s = y < 0 ? '-' : y > 9999 ? '+' : '';
  20. return s + ('00000' + Math.abs(y)).slice(s ? -6 : -4) +
  21. '-' + lz(d.getUTCMonth() + 1) + '-' + lz(d.getUTCDate()) +
  22. 'T' + lz(d.getUTCHours()) + ':' + lz(d.getUTCMinutes()) +
  23. ':' + lz(d.getUTCSeconds()) + '.' + (m > 99 ? m : '0' + lz(m)) + 'Z';
  24. } : $toISOString;