1 | 'use strict';
|
---|
2 | var fails = require('../internals/fails');
|
---|
3 | var padStart = require('../internals/string-pad').start;
|
---|
4 |
|
---|
5 | var abs = Math.abs;
|
---|
6 | var DatePrototype = Date.prototype;
|
---|
7 | var getTime = DatePrototype.getTime;
|
---|
8 | var nativeDateToISOString = DatePrototype.toISOString;
|
---|
9 |
|
---|
10 | // `Date.prototype.toISOString` method implementation
|
---|
11 | // https://tc39.es/ecma262/#sec-date.prototype.toisostring
|
---|
12 | // PhantomJS / old WebKit fails here:
|
---|
13 | module.exports = (fails(function () {
|
---|
14 | return nativeDateToISOString.call(new Date(-5e13 - 1)) != '0385-07-25T07:06:39.999Z';
|
---|
15 | }) || !fails(function () {
|
---|
16 | nativeDateToISOString.call(new Date(NaN));
|
---|
17 | })) ? function toISOString() {
|
---|
18 | if (!isFinite(getTime.call(this))) throw RangeError('Invalid time value');
|
---|
19 | var date = this;
|
---|
20 | var year = date.getUTCFullYear();
|
---|
21 | var milliseconds = date.getUTCMilliseconds();
|
---|
22 | var sign = year < 0 ? '-' : year > 9999 ? '+' : '';
|
---|
23 | return sign + padStart(abs(year), sign ? 6 : 4, 0) +
|
---|
24 | '-' + padStart(date.getUTCMonth() + 1, 2, 0) +
|
---|
25 | '-' + padStart(date.getUTCDate(), 2, 0) +
|
---|
26 | 'T' + padStart(date.getUTCHours(), 2, 0) +
|
---|
27 | ':' + padStart(date.getUTCMinutes(), 2, 0) +
|
---|
28 | ':' + padStart(date.getUTCSeconds(), 2, 0) +
|
---|
29 | '.' + padStart(milliseconds, 3, 0) +
|
---|
30 | 'Z';
|
---|
31 | } : nativeDateToISOString;
|
---|