Add two numbers and convert to time using jquery

Posted by Klbaiju under jQuery on 3/24/2014 | Points: 10 | Views : 707 | Status : [Member] | Replies : 1
Hi,

I want to add 2 numbers and convert into time format

following code returns float as correct

if (headertext == 2500)

{
var name = $(this).text();


totalHr += parseFloat(name.replace(":", "."));
}

my requirement is result should be in time format

example datas are 08:55 and 09:55

result should be 18:50 not 18.10

how to solve this




Responses

Posted by: Sumit08 on: 3/24/2014 [Member] Starter | Points: 25

Up
0
Down
try this one.i think the result same as you told in your example.


var str1 = "08:55";
var str2 = "09:55";
var token1 = str1.split(":");
var token2 = str2.split(":");
var result = parseInt(token1[0], 10) * 60 + parseInt(token1[1], 10) + parseInt(token2[0], 10) * 60 + parseInt(token2[1], 10);
var newTime = Math.floor(result/60) + ":" + (result % 60);
alert(newTime);


Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response