-2

I'm trying to add 3 minutes to the time but instead of adding to the time, it instead acted like they're two strings and added them together like they're strings. What am I doing wrong?

var minutes = 3;
console.log(date.getTime() + (minutes * 60 * 1000));
2
  • 2
    This is working for me (not behaving as strings), for example new Date(new Date().getTime() + (3 * 60 * 1000)). What are the types and values of date and data.getTime()?
    – Kobi
    Commented Jan 1, 2019 at 8:11
  • When I run your code, I don't have the problem you're describing. Commented Jan 1, 2019 at 11:40

4 Answers 4

2

To add minutes simply use the setMinutes method:

var date = new Date(/* whatever */);
var minutes = 3;
date.setMinutes(date.getMinutes() + minutes);
console.log(date)

1

It should be new Date()

console.log(new Date().getTime() + 3*60*100)

0

This should help

var minutes = 3;

let newDate = new Date().getTime() + (minutes * 60 * 1000);
console.log(new Date(newDate).toLocaleTimeString())

2
  • There is no need to create a new Date, nor to convert everything to milliseconds. You can add minutes using setMinutes.
    – RobG
    Commented Jan 1, 2019 at 22:37
  • Duly noted... Thanks a lot Commented Jan 2, 2019 at 4:25
0

const now = Date.now();
const threeMinutesFromNow = now + 3 * 60 * 1000;
console.log(`now: ${new Date(now).toLocaleTimeString()}`);
console.log(`3m from now: ${new Date(threeMinutesFromNow).toLocaleTimeString()}`);

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