'getUTCHours() Javascript returns wrong hours

I have:

var h = d.getUTCHours();

This should return h = 15. The current time is 3:50 PM. However it is returning h = 22.

Any ideas why ?

The minutes, seconds, month, day and year functions work fine for me.



Solution 1:[1]

getUTCHours gets the universal time (known as Coordinated Universal Time), which is probably not the same as your local time.

I'm in San Francisco, and my local time is 15:53. However, current UTC is 22:53.

You should use getHours.

In my example above:

var myLocalHours = new Date().getHours(); // => 15
var currentUTCHours = new Date().getUTCHours(); // => 22

Solution 2:[2]

Also please make sure you're giving the timestamp in milliseconds (if it's not multiply it by 1000), because JavaScript's expected timestamps are in millis.

I wanted this mistake to be clearly mentioned here so I added it as an answer and not as a comment for people who get wrong results and start scratching their heads :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 aderchox