Date() object, like:
const newDate = new Date(); console.log(newDate)
This Date() object returns the current date and time of the user in timestamp format
If you see the output, we have a date and we need to add some hours to it. Let’s see how can do it.
The date is an object from the date class, so we have access to the ‘getHours()’ method to get the hours of this date.
So, we can use the “
setHours” method, it returns 3 hours without adding 3 from the current hours.
So, to get current hours with the ‘getHours’ and add the hours that we want to add. If we add 3 hours to our current time, we have the hours 18 + 3 =21 hours, because the current hour is 18 and our added time is 3.
newDate.setHours(newDate.getHours() + 3) console.log(newDate)
We can add hours by using
returns the number of milliseconds since January 1, 1970, which is commonly referred to as the Unix timestamp. By adding the desired number of hours in milliseconds to the current timestamp and creating a new date object from it, we can achieve the desired result.
const currentDate = new Date(); const hoursToAdd = 3; const newDate = new Date(currentDate.getTime() + hoursToAdd * 60 * 60 * 1000); console.log(newDate);