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 <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 1d6c17e commit 6d1527b

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

‎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
returnctx.stylize(RegExp.prototype.toString.call(value),'regexp');
349349
}
350350
if(isDate(value)){
351-
returnctx.stylize(Date.prototype.toISOString.call(value),'date');
351+
if(Number.isNaN(value.getTime())){
352+
returnctx.stylize(value.toString(),'date');
353+
}else{
354+
returnctx.stylize(Date.prototype.toISOString.call(value),'date');
355+
}
352356
}
353357
if(isError(value)){
354358
returnformatError(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(newDate('Sun, 14 Feb 2010 11:48:40 GMT')),
15+
assert.strictEqual(util.inspect(newDate('Sun, 14 Feb 2010 11:48:40 GMT')),
1616
newDate('2010-02-14T12:48:40+01:00').toISOString());
17+
assert.strictEqual(util.inspect(newDate('')),(newDate('')).toString());
1718

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

0 commit comments

Comments
 (0)