Skip to content

Commit 6d1527b

Browse files
rumkinevanlucas
authored andcommitted
util: fix invalid date output with util.inspect
Prevent util.inspect of throwing on date object with invalid date value. It changed to output result of toString method call. PR-URL: #6504 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 1d6c17e commit 6d1527b

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

lib/util.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,11 @@ function formatValue(ctx, value, recurseTimes) {
348348
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
349349
}
350350
if (isDate(value)) {
351-
return ctx.stylize(Date.prototype.toISOString.call(value), 'date');
351+
if (Number.isNaN(value.getTime())) {
352+
return ctx.stylize(value.toString(), 'date');
353+
} else {
354+
return ctx.stylize(Date.prototype.toISOString.call(value), 'date');
355+
}
352356
}
353357
if (isError(value)) {
354358
return formatError(value);

test/parallel/test-util-inspect.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ assert.equal(util.inspect(function() {}), '[Function]');
1212
assert.equal(util.inspect(undefined), 'undefined');
1313
assert.equal(util.inspect(null), 'null');
1414
assert.equal(util.inspect(/foo(bar\n)?/gi), '/foo(bar\\n)?/gi');
15-
assert.equal(util.inspect(new Date('Sun, 14 Feb 2010 11:48:40 GMT')),
15+
assert.strictEqual(util.inspect(new Date('Sun, 14 Feb 2010 11:48:40 GMT')),
1616
new Date('2010-02-14T12:48:40+01:00').toISOString());
17+
assert.strictEqual(util.inspect(new Date('')), (new Date('')).toString());
1718

1819
assert.equal(util.inspect('\n\u0001'), "'\\n\\u0001'");
1920

0 commit comments

Comments
 (0)