I have a string containing 13:00 , i have an duration of 90 for example and i have to convert the duration to hh:mm which i made it. Now i have a string with 1:30. How can i add this 1:30 string to the other 13:00 string so i can compare it with another strings? This is how i conver the duration
var bookH = Math.floor(data[0]['Duration'] / 60);
var bookM = data[0]['Duration'] % 60;
var bookingDurationToHour = bookH + ':' + bookM;
There is momentjs in the project but i still do not know how can i do that.
A solution with plain javascript.
var time = "13:00";
var totalInMinutes = (parseInt(time.split(":")[0]) * 60) + parseInt(time.split(":")[1]);
var otherMinutes = 90;
var grandTotal = otherMinutes + totalInMinutes;
//Now using your own code
var bookH = Math.floor(grandTotal / 60);
var bookM = grandTotal % 60;
var bookingDurationToHour = bookH + ':' + bookM;
alert(bookingDurationToHour);
Plunker
I've used Datejs before as vanilla JS with dates can be a bit difficult
First you want to put your time into a date time object
var myDate = Date.parse("2017-05-25 " + time);
Then you can add your time using the Datejs library:
myDate = myDate.addMinutes(90);
Then toString your new date as a time:
var newTime = myDate.toString("HH:MM");
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