0

I have date in string:

2020-12-15T07:35:33.000Z

I need get hour by call new Date("2020-12-15T07:35:33.000Z").getHours()

On server, I am getting a hour from this time - and it is 7.

On client, I am getting a hour from this time - and it is 8.

Why? I need to get same hour on server, because I am building data for my charts to show on client side.

What should I do to get same hour on server like on client?

yihereg819
  • 131
  • 1
  • 6
  • 2
    `getUTCHours()` will return the same number. Your server and client are in different time zones. – VLAZ Dec 22 '20 at 18:58
  • And in case I would like to work with client timezone on server, what should I use? – yihereg819 Dec 22 '20 at 19:02
  • Related: https://stackoverflow.com/q/15141762/2657515 – JonathanDavidArndt Dec 22 '20 at 19:18
  • `getUTCHours` returns still wrong hour on server – yihereg819 Dec 22 '20 at 20:08
  • @VLAZ getUTCHours doesnt work – yihereg819 Dec 22 '20 at 20:43
  • 1
    @yihereg819 if you get different times, then you're not running the code you've shown. The timestamp you have is in UTC, so getting the UTC time must return the same number regardless of the timezone the machine is in. Since you say that's not happening, you must have something different. I suspect you're using a timestamp that's not in UTC, so it's going to be interpreted as local. – VLAZ Dec 22 '20 at 21:03
  • make sure working with UTC 0 on the server, when the client request the date add or remove hours from the local time zone to the server time zone – Proxytype Dec 22 '20 at 22:17
  • @VLAZ—the server could be set to say Europe/London where the offset in Dec is 0 but in say Jun is +1, so *getHours* will return the same hour when in standard time but not in daylight saving time. – RobG Dec 22 '20 at 22:31

0 Answers0