Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does setTime add an hour?

Tags:

javascript

This code results in an output of 1. Why is this when the argument to setTime is the number of milliseconds added to midnight Jan 1 1970? Surely it should be 0?

var d = new Date();

d.setTime(0);

console.log(d.getHours());
like image 483
Ian Warburton Avatar asked May 18 '26 17:05

Ian Warburton


1 Answers

you can do this

    var d = new Date();
    
    d.setTime(0);
    
    console.log(d.getUTCHours());

thing is getHours give time at your timezone(local timezone), to get the UTC time just change it to getUTCHours

like image 115
ashish singh Avatar answered May 20 '26 05:05

ashish singh