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!

5

3 Answers 3

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));

1
  • new Date('2017-06-16') will be treated as UTC, likely the OP wants local.
    – RobG
    Commented Jun 17, 2017 at 13:17
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 )

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));

1
  • 3
    You're not using the date parameter Commented Jun 16, 2017 at 22:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.