1

What I'm trying to do is to add 50 minutes to the current date I get, so I want to get the date and then get the time and then add 50 minutes. I want to check if I'm doing it the right way:

d = new Date();
dateAfter50min = d.setDate(d.getMinutes() + 50);
Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
develop05
  • 467
  • 1
  • 8
  • 19
  • 1
    _"I want to check if I'm doing it the right way"_ - This can be checked in seconds with a simple `console.log()` – Andreas Sep 30 '18 at 17:44
  • Yes, thank you :) I mean if there is a more proper way for doing this. – develop05 Sep 30 '18 at 17:46
  • 1
    This might help: [How to add 30 minutes to a JavaScript Date object?](https://stackoverflow.com/questions/1197928/how-to-add-30-minutes-to-a-javascript-date-object) – Luan Moraes Sep 30 '18 at 17:51

2 Answers2

1

If you want to just modify your current date object you can do it by following code

var d = new Date();
console.log(d);
d.setMinutes(d.getMinutes() + 50);
console.log(d);
Chayan
  • 554
  • 4
  • 10
0

If you want to create a new Date by adding minutes to an existing the date, you can get the time in milliseconds, and then add the minutes converted to milliseconds - minutes * 60 * 1000:

const d = new Date();
const dateAfter50min = new Date(d.getTime() + 50 * 60 * 1000);

console.log(d);
console.log(dateAfter50min);
Ori Drori
  • 166,183
  • 27
  • 198
  • 186