Skip to content

Initialize JavaScript date to midnight

To set JavaScript date to midnight, we can use the setHours() method. Set hours, minutes, seconds and milliseconds to zeroes.

Advertisements
var dt = new Date();
dt.setHours(0,0,0,0);
console.log(dt.toString());
//Thu Oct 01 2020 00:00:00 GMT+0530 (India Standard Time)

To set the date to next midnight, you can set hours as 24 in the setHours() method.

var dt = new Date();
dt.setHours(24,0,0,0);
console.log(dt.toString());
//Fri Oct 02 2020 00:00:00 GMT+0530 (India Standard Time)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.