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
  • Possible duplicate of How to add 30 minutes to a JavaScript Date object? Commented Jun 16, 2017 at 22:26
  • 2017-06-16 isn't a date object or a string. Commented Jun 16, 2017 at 22:29
  • w3schools.com/jsref/jsref_obj_date.asp Commented Jun 16, 2017 at 22:31
  • "…but it did not work" is not helpful. What result did you get? What error messages were in the console? What result did you expect? Commented Jun 18, 2017 at 12:38
  • People not responding to the answer. Commented Jun 18, 2017 at 19:00

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

Sign up to request clarification or add additional context in comments.

1 Comment

new Date('2017-06-16') will be treated as UTC, likely the OP wants local.
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 )

Comments

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 Comment

You're not using the date parameter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.