es6.date.to-iso-string.js 1009 B

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