1

When I do new Date() on my browser's console. I will get the time that is on my device. Like this Thu Dec 16 2021 17:07:55 GMT+0530 (India Standard Time) But when I do new Date in my node project I got a different time. 2021-12-16T11:37:40.931Z

what do I have to do to get the time which I am getting in the browser's console? I think there is some timeZone issue. but I did not change anything. So why did this happen?

  • https://github.com/nodejs/node/issues/19480 – jabaa Dec 16 '21 at 11:46
  • The first is local to your PC settings (IST). The second is UTC, so different by your local offset of +5:30. They are just different representations of the same moment in time (though it seems there is about 15 seconds difference in when the Dates were created). – RobG Dec 16 '21 at 12:09
  • the 15-second difference is from my side. cause I am creating questions so. I know one is IST and the other is UTC. but why there is a difference in the same code. that's the main question. I think I got the answer in @jabaa – SAGAR SOLANKI Dec 16 '21 at 12:18
  • Probably a duplicate of [*How come my javascript (node.js) is giving me the incorrect timestamp?*](https://stackoverflow.com/questions/7481963/how-come-my-javascript-node-js-is-giving-me-the-incorrect-timestamp?r=SearchResults&s=3|64.6479) There is no specification for how consoles should behave, e.g. the console in SO shows UTC (likely from *toISOString*), but the default for my browser console is *Date.prototype.toString* i.e. local. Meh. – RobG Dec 16 '21 at 12:19

1 Answers1

0

This should "convert" from UTC to your timezone (new Date()).toString()

CHNguyen
  • 164
  • 1
  • 1
  • 7
  • yes, but why now do we have to do this? – SAGAR SOLANKI Dec 16 '21 at 12:19
  • 1
    @SAGARSOLANKI Because the format in `console.log` isn't standardized. The date has to be serialized. Some implementations serialize it in the local timezone and other in UTC. The date object contains the same value, only `console.log` represents it differently. – jabaa Dec 16 '21 at 13:06