1//JS
2function addHoursToDate(date, hours) {
3 return new Date(new Date(date).setHours(date.getHours() + hours));
4}
5
6//TS
7function addHoursToDate(date: Date, hours: number): Date {
8 return new Date(new Date(date).setHours(date.getHours() + hours));
9}
10
11let myDate = new Date();
12
13console.log(myDate)
14console.log(addHoursToDate(myDate,2))
1<html>
2 <head>
3 <title>JavaScript setHours Method</title>
4 </head>
5 <body>
6 <script>
7 var dt = new Date();
8 dt.setHours( dt.getHours() + 2 );
9 document.write( dt );
10 </script>
11 </body>
12</html>
1Date.prototype.addHours = function(h) {
2 this.setTime(this.getTime() + (h*60*60*1000));
3 return this;
4}