I want to convert a 12-hour format time into a string, add some minutes, then convert back to string. My user will be entering a time as a sting example 8:03 AM this time needs to converted into an integer with 9 minutes added to it then converted back to the same 12-hour format just 9 minutes into the future.
I've tried to make a Date obj using the string time, but when I do the Date constructor says I am using an invalid date.
new Date("8:03 AM")
What I tried after that was adding a speicifc date to the time
new Date("09/10/2022 8:03 AM")
That worked to create a Date obj and then I would try to get just the time using .getTime() and pass that into my intToHHMM function.
const intToHHMM = function (time) {
var sec_num = parseInt(time, 10); // don't forget the second param
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes;
}
But I am getting a weird return that definitely is not hh:mm format.
Can someone lend a hand, I appreciate the help in advance!