Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows PowerShell doesn't console.log the Date object from Node.js

I am trying to console.log Date object in Windows PowerShell by executing Node.js:

node -e "var d = new Date();console.log('new date = ', d);"

However, the PowerShell doesn't give me any date. The only thing I am getting is:

new date=

All other type of data get printed out, including time.

The only way I found to see the date in PowerShell is to add .toString() to my code:

node -e "var d = new Date();console.log('new date = ', d.toString());"

Then I get: dateAsStringImage

I don't have this problem in a simple CMD on the same system (Windows10Pro). The above code (node -e "var d = new Date();console.log('new date = ', d);") gets printed:

new date= 2020-03-14T22:46:49.847Z

If anyone has any idea how to solve this, I will appreciate a lot your help.

like image 494
KaroDev Avatar asked Sep 17 '25 17:09

KaroDev


1 Answers

The problem is merely a display problem:

The date string does print, but effectively invisibly, because the foreground (text) color chosen is apparently the same as PowerShell's background color.

A simple workaround is to pipe to Write-Output, which seems like a no-op, but actually causes node to suppress the coloring, so that the text prints normally:

PS> node -e "var d = new Date();console.log('new date = ', d);" | Write-Output
new date =  2020-03-15T03:07:06.798Z

Similarly,

  • redirecting the output to a file would capture the text as expected (without the VT (Virtual Terminal) sequences that produce the colors in the console).

  • copying and pasting the output line reveals the complete text.

  • changing the console's background color to something like light green makes the date string directly visible in the output.

like image 110
mklement0 Avatar answered Sep 19 '25 07:09

mklement0