need some help with adding extra time to current time
currentTime= new Date();
var hours2 = currentTime.getHours() + 4;
var minutes2 = currentTime.setMinutes(currentTime.getMinutes() + 30);
however it outputs as: 15:1455880112692PM and does not seem to be adding 4.5 hours? anyone have any ideas on this?
That’s because you are not getting the minutes, you are getting the Date object. The
setMinutesmethod doesn’t return the minutes, it returns the Date object itself. Converting a Date object to a string to display it gives you the time in milliseconds since epoch.First convert the time, so that you get a correct time that wraps over, and not something like
27:93instead of04:33.Then you can get the hours and minutes from it:
Instead of adding 4 hours and 30 minutes, you can add 270 minutes: