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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get time elapsed from 2 textboxes?

Status
Not open for further replies.

arneweise77

Programmer
Apr 25, 2002
46
SE
Hi!

I have this "tricky" problem regarding total time elapsed.
Im using a form with 2 textboxes: Start-time, End-time which the user fills in values in string format. Let´s say start-time= 23:25:45, and end-time= 02:26:46. Now I want to present total time elapsed as = 3:01:01 in a third textbox.

Im using ASP.NET, and have a java script function for this problem. I´ve tried many ways to separate the string into parts, and do get them the separated. But I dont get the correct total time elapsed.

Is there a function to use with this problem, or can anyone help me with this? I´ll show you my code aswell.
Thanx in advance. Arne

Here is the code:

<script language=&quot;javascript1.2&quot; type=&quot;text/javascript&quot;>

function SetOperationTime()
{
if ((window.form.starttxt.value.length > 0)) && (window.form.stoptxt.value.length > 0))
{
var start = window.form.starttxt.value
var end = window.form.stoptxt.value

var startsplit = start.split(&quot;:&quot;)
var endsplit = end.split(&quot;:&quot;)

if ((endsplit[0]) < (startsplit[0]))
{
endsplit[0] = 24 - endsplit[0]

if ((endsplit[1] ) < (startsplit[1]))
{
endsplit[1] = 60 - endsplit[1]
endsplit[0] = endsplit[0] - 1
}

if (endsplit[2] < startsplit[2])
{
endsplit[2] = 60 - endsplit[2]
endsplit[1] = endsplit[1] - 1
}
}

var sum = ((endsplit[0] - startsplit[0]) +&quot;:&quot;+ (endsplit[1] - startsplit[1]) +&quot;:&quot;+ (endsplit[2] - startsplit[2]))
var sumsplit = sum.split(&quot;:&quot;)

var sum_count = 1
while ((sumsplit[sum_count] < 10) && (sum_count < 3))
{
sumsplit[sum_count] = &quot;0&quot; + sumsplit[sum_count]
sum_count +=1
}
window.form.operationtxt.value = sumsplit[0] +&quot;:&quot;+ sumsplit[1] +&quot;:&quot;+ sumsplit[2]
}
else
{
window.form.operationtxt.value = &quot;0:00:00&quot;
}
}

</script>
 
Try this instead for setting up your time:

if (endsplit[2] < startsplit[2])
{
endsplit[2] = 60 + (endsplit[2] * 1);
endsplit[1]--;
}

if ((endsplit[1] ) < (startsplit[1]))
{
endsplit[1] = 60 + (endsplit[1] * 1);
endsplit[0]--;
}

if (endsplit[0] < startsplit[0])
{
endsplit[0] = 24 + (endsplit[0] * 1);
}
 
After a few changes i got to this:

<script language=&quot;javascript&quot;>
function SetOperationTime(){
if (window.document.xpto.starttxt.value.length > 0 && window.document.xpto.stoptxt.value.length > 0)
{
var start = window.document.xpto.starttxt.value
var end = window.document.xpto.stoptxt.value

var startsplit = start.split(&quot;:&quot;)
var endsplit = end.split(&quot;:&quot;)
var diff=new Array()

diff[0]=endsplit[0]-startsplit[0]
diff[1]=endsplit[1]-startsplit[1]
diff[2]=endsplit[2]-startsplit[2]

if(diff[0]<0) diff[0]+=24
if (diff[1]<0){
diff[1]+=60
diff[0]--
}
if (diff[2]<0){
diff[2]+=60
diff[1]--
}
window.document.xpto.operationtxt.value = diff[0] +&quot;:&quot;+ diff[1] +&quot;:&quot;+ diff[2]
}
else
{
window.document.xpto.operationtxt.value = &quot;0:00:00&quot;
}
}
</script>

<form name=xpto>
<input type=text name=starttxt><br>
<input type=text name=stoptxt><br>
<input type=text name=operationtxt>
<input type=button onclick=&quot;SetOperationTime()&quot; value=&quot;count&quot;>
</form>

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Isn't that stupid that JavaScript doesn't just propose a dateDiff or timeDiff function?

Just a thought...
 
You should check seconds before minutes before hours to get an accurate time difference.
 
Thanks guys, you´ve been most helpful, and I´m glad to say: problem solved!

Thanks again, maybe we´ll run into eachother some other day
Arne Weise
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top