1

NodeJS outputs incorrect Date value in console.log

Assuming: var todayDate = new Date();

Machine timezone: GMT+0530 / IST

Following are results:

#1

console.log ('Today date:', todayDate);

Output: 020-04-04T09:54:29.107Z

#2 console.log ('Today date as UTC:', todayDate.toUTCString());

Output: Today date as UTC: Sat, 04 Apr 2020 09:54:29 GMT

#3 console.log ('Today date as string:', todayDate.toString());

Output: Today date as string: Sat Apr 04 2020 15:24:29 GMT+0530 (India Standard Time)

I am getting Output#1 whereas expected output is #3.

The Chrome browser shows same result as #3 which I expected with NodeJS since it runs on Chrome engine.

Please explain why NodeJS not output in local date/time.

3
  • What output are you getting instead of the expected one? I get the exact same output in node (v12) and in my chrome browser, i.e. Sat Apr 04 2020 12:45:57 GMT+0200 (Central European Summer Time) in my case. Commented Apr 4, 2020 at 10:46
  • I am getting Output#1 whereas expected output is #3. Commented Apr 4, 2020 at 10:54
  • Ah now I get it - interesting, didn't know that. Seems the default formatter differs in the implementations. Commented Apr 4, 2020 at 11:03

2 Answers 2

2

Fundamentally the Date object is the same in both chrome and node. It contains the number of seconds elapsed since the UNIX epoch. However different platforms chose to display it differently. Node.js calls toISOString(), while Chorome calls toString() (toString() in Node.js will return the same as in Chrome).

Sign up to request clarification or add additional context in comments.

5 Comments

Please provide a reference to support your answer. Is it mentioned in NodeJS documentation?
I don't think it's mentioned anywhere. In the code base github.com/nodejs/node/blob/…, you can see it gets formatted with Date.toString() if the Date.getTime() is NaN, and with Date.toISOString() otherwise. Playing around with the code a little bit, apparently Date.toISOString() throws and error when the time is NaN and toString() returns 'Inavlid Date' in V8.
You can actually try it (new Date(undefined)).toISOString() -> Error and (new Date(undefined)).toString() -> 'Invalid Date'. new Date(undefined) prints 'Invalid Date', so it's formated with toString(). So according to the code base it gets formated with toString() if the time is NaN, toISOString() otherwise.
This is the answer I was looking for, after setting TZ in docker, and confirming it was fine in the image... I was so perplexed where the UTC was coming from with all my custom settings. ty
0

I can confirm that behaviour. In my case I was looping through unix dates from database:

    console.log(endunix); //epoch date
    console.log(endutc); // wrong date that will be fixed from unixdate
    let nowadatautc = new Date(endunix*1000);
    console.log(nowadatautc); // new date
let sql2 = "update schedule a set end_time_utc='"+nowadatautc+"' where a.`event_id` > 374 and a.`sport_id` = 5";

and I get:

1645328100
2022-02-20T03:05:00.000Z
2022-02-20T03:35:00.000Z
update schedule a set end_time_utc='Sun Feb 20 2022 04:35:00 GMT+0100 (czas środkowoeuropejski standardowy)' where a.`event_id` > 374 and a.`sport_id` = 5

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.