Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid date output with util.inspect #6504

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/util.js
Expand Up @@ -304,7 +304,11 @@ function formatValue(ctx, value, recurseTimes) {
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
}
if (isDate(value)) {
return ctx.stylize(Date.prototype.toISOString.call(value), 'date');
if (Number.isNaN(value.getTime())) {
return ctx.stylize(value.toString(), 'date');
} else {
return ctx.stylize(Date.prototype.toISOString.call(value), 'date');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a style nit so feel free to ignore...

return ctx.stylize(Number.isNaN(value.getTime()) ?
                     value.toString() :
                     Date.prototype.toISOString.call(value),
                   'date');

... would help eliminate some minor code duplication

}
if (isError(value)) {
return formatError(value);
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-util-inspect.js
Expand Up @@ -12,8 +12,9 @@ assert.equal(util.inspect(function() {}), '[Function]');
assert.equal(util.inspect(undefined), 'undefined');
assert.equal(util.inspect(null), 'null');
assert.equal(util.inspect(/foo(bar\n)?/gi), '/foo(bar\\n)?/gi');
assert.equal(util.inspect(new Date('Sun, 14 Feb 2010 11:48:40 GMT')),
assert.strictEqual(util.inspect(new Date('Sun, 14 Feb 2010 11:48:40 GMT')),
new Date('2010-02-14T12:48:40+01:00').toISOString());
assert.strictEqual(util.inspect(new Date('')), (new Date('')).toString());

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

Expand Down