Reputation: 363
i want to add 1010 minutes to my time: 12 am
. the final time should be: 4:50 pm
. The date should NOT matter.
i tried with this:
function AddMinutesToDate(date, minutes) {
return new Date(date.getTime() + minutes*60000);
}
alert(AddMinutesToDate(2017-06-16), 1010)
but it did not work. please help? thanks!
Upvotes: 1
Views: 5602
Reputation: 313
This function will accept ISO format and also receives minutes as parameter.
function addSomeMinutesToTime(startTime, minutestoAdd) {
const dateObj = new Date(startTime);
const newDateInNumber = dateObj.setMinutes(dateObj.getMinutes() + minutestoAdd);
const processedTime = new Date(newDateInNumber).toISOString();
console.log(processedTime)
return processedTime;
}
addSomeMinutesToTime(("2019-08-06T10:28:10.687Z"), 1010 )
Upvotes: 1
Reputation: 76
You can do it like that : (sorry Will I can't edit your code because the change is less than 6 characters...)
function AddMinutesToDate(date, minutes) {
return new Date(new Date(date).getTime() + minutes * 60000);
}
alert(AddMinutesToDate('2017-06-16', 1010));
Upvotes: 1
Reputation: 3251
You were pretty close. Just a couple of errors.
function AddMinutesToDate(date, minutes) {
return new Date(new Date().getTime() + minutes * 60000);
}
alert(AddMinutesToDate('2017-06-16', 1010));
Upvotes: 0