Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add / calculate time values - hours and minutes

Status
Not open for further replies.

Ranvier

Programmer
Jun 17, 2004
73
GB
Hi,

This may seem like a simple question, but...

I am trying to add two time values together. For exmple my first value is 0:59 (being 59 minutes) and my second value is 2:36 (being 2 hrs and 36 minutes) which should make a total of 3:35
If I use a parseInt my values are not treated as time. Can someone tell me how to correctly parse these values so I can add them up please.

Thanks in advance
 
Hi

There is no time object in JavaScript, so you either adapt the problem for the [tt]Date[/tt] object, or just calculate "by hand" :
Code:
[b]function[/b] timeAdd(s1,s2)
{
  [b]var[/b] a1=s1.split(/:/),a2=s2.split(/:/)
  [b]var[/b] v=[b]new[/b] Array(parseInt(a1[0])+parseInt(a2[0]),parseInt(a1[1])+parseInt(a2[1]))
  [b]return[/b] (v[0]+Math.floor(v[1]/60))+[i]':'[/i]+v[1]%60
}

[gray]// ... [/gray]

timeAdd('0:59','2:36')

Feherke.
 
Just what I needed, thanks very much.

R
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top