I know that the topic is way too old. But I am pretty sure that there are some developpers who still need this, so I made this simple script for you. I hope you enjoy it!
Hello back, It's 2020 and I've added some modification hope it will help a lot better now!
Hello again, It is 2022 and I came back again to fix some issues and give a better naming for the methods & functions.
function addTimeToDate(addedTime, date){
let generatedTime=dategeneratedTime = date.getTime();
if(addedTime.seconds) generatedTime+=1000*addedTimegeneratedTime += 1000 * addedTime.seconds; //check for additional seconds
if(addedTime.minutes) generatedTime+=1000*60*addedTimegeneratedTime += 1000* 60 * addedTime.minutes;//check for additional minutes
if(addedTime.hours) generatedTime+=1000*60*60*addedTimegeneratedTime += 1000 * 60 * 60 * addedTime.hours;//check for additional hours
return new Date(generatedTime);
}
Date.prototype.addTime = function(addedTime){
return addTimeToDate(addedTime, this);
}
let futureDate = new Date().addTime({
hours: 16, //Adding one hour
minutes: 45, //Adding fourty five minutes
seconds: 0 //Adding 0 seconds return to not adding any second so we can remove it.
});
<button onclick="console.log(futureDate)">Travel to the future</button>