0

my js file looks like below

var today = Date();
console.log(today); // Thu Aug 04 2022 15:28:52 GMT+0530 (India Standard Time)


var today = new Date();
console.log(today); // 2022-08-04T09:58:52.640Z

I have set the local timezone in my centos to Asia/Calcutta and its Date() function is displaying correctly as Aug 04 2022 15:28:52 , how do I make even new Date() to output the same data in my nodejs script.

Gracie williams
  • 1,287
  • 2
  • 16
  • 39
  • Does this answer your question? [Difference between Date(dateString) and new Date(dateString)](https://stackoverflow.com/questions/3505693/difference-between-datedatestring-and-new-datedatestring) – derpirscher Aug 04 '22 at 10:13
  • And printing the ISO UTC representation of a date object, when you do console log is just the default behaviour of the Node REPL ... – derpirscher Aug 04 '22 at 10:15

1 Answers1

0

I think what you're looking for is pretty well described on this topic : How to initialize a JavaScript Date to a particular time zone

To summarize, you only have to do today.toLocaleString('IN', { timeZone: 'Asia/Kolkata' }) to get the time in India. Please note that I'm not sure I picked the good timezone, so you might need to adjust that.

liguepk
  • 191
  • 6
  • The problem is not the timezone, the problem is 1) two different initializations of a Date which return different things (one returns a `string`, the other returns a `Date` object) and 2) the default behaviour of the node REPL (which defaults to `adateobject.toISOString()` on `console.log(adateobject)` – derpirscher Aug 04 '22 at 11:14
  • Since the title of your post is 'set timezone in nodejs' and in your example the two times are not the same, I think this is misleading. Anyway if the issue is the output format, you can do `today.toString()` which will output the same data as when you use Date() – liguepk Aug 04 '22 at 11:23