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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With