I want to get the time on the computer of the person who is accessing my website. Should I use getHours() or getUTCHours()?
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()
I would recommend getUTCHours
- keeping everything in UTC removes a lot of headaches when it comes to working with dates and times.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With