Search code examples
javascriptdatetimer

How to set the date to be 3 minutes ahead


I need a timer for my program so my idea was to use the Date() and a future date to make the difference between them a timer,but i have been having problems with Date() functions,where the future returns null

var date = new Date();
var future = date.setMinutes(date.getMinutes + 3);

console.log(future); //prints NAN

var distance = future - date;
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

Solution

  • use getMinutes as a method call and add 3 to that.

    let future = new Date((date = new Date()).setMinutes(date.getMinutes() + 3));
    

    let future = new Date((date = new Date()).setMinutes(date.getMinutes() + 3));
    console.log(future.toLocaleString());