Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript setHours date set to previous date

I want to set time to 0 using javascript's setHours() method but it changes the day of the month to one less than the actual day. Sample code:

var d = new Date();  //2017-09-18T04:58:34.223Z
d.setHours(0,0,0,0);  //2017-09-17T18:30:00.000Z
like image 547
Manoj Sanjeewa Avatar asked Sep 18 '17 05:09

Manoj Sanjeewa


1 Answers

The setHours() method sets the hours for a specified date according to local time, and returns the number of milliseconds since 1 January 1970 00:00:00 UTC until the time represented by the updated Date instance.

To set the time to midnight in universal time ,use setUTCHours().

The getUTCHours() method returns the hours in the specified date according to universal time.

var date = new Date();
console.log(date.toISOString());
date.setUTCHours(0,0,0,0);
console.log(date.toISOString());
like image 172
Hassan Imam Avatar answered Sep 21 '22 12:09

Hassan Imam