Printing the current date in Node with your current timezone offset


You’ll notice that if you use the browser’s dev tools console and type in new Date() you’ll get the current date printed in a nice human readable string which should be the current time in your location.

-> new Date()
<- Mon Apr 01 2019 23:33:08 GMT-0500 (Central Daylight Time)

But what happens when you try to do the same thing in Node?
You’d expect the same output, but thats not what you get.

> new Date()

Why does it look like this? Its because when you output a Date object in the browser, the browser will by default use Date.toString() which will give it that nice, human readable look.
In Node however, Dates are by default outputted with Date.toISOString() which returns a hard to read notated form of the current date without any timezone offset (UTC+0).

To get around this you can simply manually call Date.toISOString().

Then when you run it, you will see the current date formatted nicely with your current timezone offset.

> new Date().toString()
'Mon Apr 01 2019 23:55:26 GMT-0500 (CDT)'

Or you could write a javascript file:

// A javascript file called date.js
console.log(new Date().toString());

And then do node date.js