Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use getHours() or getUTCHours for my Javascript?

I want to get the time on the computer of the person who is accessing my website. Should I use getHours() or getUTCHours()?

like image 340
user950891 Avatar asked Nov 06 '11 00:11

user950891


4 Answers

Short answer is it depends. If you want the hours as it displays on a clock for their timezone you will want getHours() as it returns the hours in their local time. If you want the hours in Universal Time (no timezone adjustment) then use getUTCHours()

like image 200
Adrian Avatar answered Nov 05 '22 15:11

Adrian


I would recommend getUTCHours - keeping everything in UTC removes a lot of headaches when it comes to working with dates and times.

like image 20
Andrew Hare Avatar answered Nov 05 '22 15:11

Andrew Hare


getHours will return the time according to their timezone, getUTCHours will get their time converted to UTC. Would probably be easier to use getUTCHours so everyone is returning their time in the same timezone.

like image 2
mpen Avatar answered Nov 05 '22 15:11

mpen


It depends what you're using it for. If you want to get the user's local time, use getHours(). If you want to get something like a time offset (say, "hours until the new CoD is released in the UK"), use getUTCHours() as an absolute reference point. Bear in mind, however, that if the user's clock is set wrong, getUTC*() functions will return times that aren't really UTC - all they do really is remove the getGMTOffset() amount for you.

like image 2
Niet the Dark Absol Avatar answered Nov 05 '22 13:11

Niet the Dark Absol