1

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!

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Jason Bale
  • 363
  • 2
  • 7
  • 14

3 Answers3

1

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));
Kaenn
  • 76
  • 3
1

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 )
Kushal Atreya
  • 183
  • 2
  • 7
0

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));
Will
  • 3,201
  • 1
  • 19
  • 17