This method is not only able to change the hours of the Date object but also to change the seconds and minutes of Date. You can read the syntax and usage of this method here.
// Create a new Date object whose time is now let day = new Date() // Set a Date’s hours, minutes, and seconds to 0, also milliseconds day.setHours(0,0,0,0) console.log(day)
Thu Oct 27 2022 00:00:00 GMT+0700
The example above indicates that we have set a Date’s hours, minutes, and seconds to 0. Additionally, this method will return the time in seconds (epoch Unix time) of that date object after it has changed. For instance, if we print out the value of the method:
// Create a new Date object whose time is now let day = new Date() // Set a Date’s hours, minutes, and seconds to 0 also milliseconds and assign the result to variable result let result = day.setHours(0,0,0,0) console.log(result)
In the next section, we are going to introduce you to a fast solution that can help you set the date’s hours, minutes, and seconds to 0 at the same time as you are creating it.
Using new Date()
This new Date() constructor is somehow different from Date() constructor. However, new Date() method is the method that is more used to create a Date() by most programmers than the Date() one.
// Create a new Date representing 2nd Nov 2025 let day = new Date(2025,10,2,0,0,0,0) console.log(day)
Sun Nov 02 2025 00:00:00 GMT+0700
Never forget to subtract 1 to your desired month to get the correct monthIndex (10 is November) since this index begins at 0 for January (not 1 as you may expect).
This method is easy to understand, but it can only be used when you have an idea of what your desired date is. Otherwise, if it is just a Date object in which you don’t know the date, then you can use the first solution instead or read more guides in our tutorial to get the date properties of that Date object.
Maybe you are interested:
Name of the university: HCMUT