How to add to the date 15 minutes? I try this way, but it does not work

let date = new Date(); let h = date.getHours(), m = date.setMinutes(date.getMinutes() + 15), res = h + ':' + m; $('.you-loan .js-loan').html(' ' + res); 
  • one
    You first change the values, and then extract the h and m that you output. Yes, and in the documentation something is not visible that setMinutes returns something. - teran

1 answer 1

This is how it works:

  var CurrentTime = new Date(); CurrentTime.setMinutes(CurrentTime.getMinutes() + 15); console.log(CurrentTime.getHours()+":"+CurrentTime.getMinutes()); 

Your problem is that setMinutes returns the value in milliseconds between January 1, 1970 00:00:00 UTC and the updated date. It is more correct to use getMinutes ().

In addition, it is important that getHours and getMinutes are called after changing the date - and not before.

  • why does that work? What exactly has changed compared to the code in the question? - Grundy
  • one
    @Grundy, the extraction of hours has changed ... And with the minutes the joint was ... And anyway, why such a question? - Qwertiy
  • one
    @Qwertiy, so that the author of the answer added a description to his answer :-) - Grundy
  • @Grundy, why?) - Qwertiy
  • one
    @Qwertiy, because otherwise both the question and the answer are useless, and they should be deleted! - Grundy