Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add hour and minutes to a string with hh:mm?

Tags:

javascript

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.

like image 312
Expressingx Avatar asked Feb 02 '26 15:02

Expressingx


2 Answers

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

like image 64
Akhil Avatar answered Feb 05 '26 06:02

Akhil


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");
like image 44
Robbie Avatar answered Feb 05 '26 06:02

Robbie