How do I add minutes to this. I followed the documentation but somehow this is not working:
var hours = randomIntFromInterval(0,23);
var minutes = randomIntFromInterval(0,59);
var time = moment(hours+':'+minutes,'HHmm').format("HH:mm");
time.add(7,'m');
Only the last line is not working, but should be right according to the documentation. What am I doing wrong?
Try: moment({ // Options here }). format('HHmm') . That should give you the time in a 24 hour format.
format
returns a string, you have to use add
on moment object.
Your code could be like the following:
var hours = randomIntFromInterval(0,23);
var minutes = randomIntFromInterval(0,59);
var time = moment(hours+':'+minutes,'HH:mm');
time.add(7,'m');
console.log(time.format("HH:mm"));
Note that you can create a moment object using moment(Object)
method instead of parsing a string, in your case:
moment({hours: hours, minutes: minutes});
As the docs says:
Omitted units default to 0 or the current date, month, and year
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With