-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));
Salman A
  • 262,204
  • 82
  • 430
  • 521
frosty
  • 2,559
  • 8
  • 37
  • 73
  • 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 Jan 01 '19 at 08:11
  • When I run your code, I don't have the problem you're describing. – David Walschots Jan 01 '19 at 11:40

4 Answers4

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)
Salman A
  • 262,204
  • 82
  • 430
  • 521
1

It should be new Date()

console.log(new Date().getTime() + 3*60*100)
Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
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()}`);
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
0

This should help

var minutes = 3;

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