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!

Calculating the difference between 2 days 1

Status
Not open for further replies.

ejrhodes

MIS
Jul 9, 2001
20
US
With some help, I wrote this simple function which takes the 2 values of text boxes and calculates the difference. The problem is the time calculation is not always correct. Any insight?

<HTML>
<HEAD>
<TITLE>Ticket Tracker</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function ResolvedDuration(date1, date2)
{
var sec = 1000;
var min = sec * 60;
var hour = min * 60;
var day = hour * 24;
var milliDiff = new Date(date2) - new Date(date1);
var numDay = Math.floor(milliDiff/day);
milliDiff -= numDay * day;
var numHour = Math.floor(milliDiff/hour);
milliDiff -= numHour * day;
var numMin = Math.floor(milliDiff/min);

var total= (numDay + ":" + numHour + ":" + numMin);
return total;

}
//--></script>
</HEAD>

<form method="post" action="ticketform.asp">
Assigned:
<input type="text" name="Assign" SIZE="20" value="">
<br>
Resolved:
<input type="text" name="ResolvedDate" SIZE="20" value="">
<br>
Duration:

<input type="text" name="Resolved_Duration" SIZE="20" value=""

onBlur='this.form.Resolved_Duration.value=ResolvedDuration(this.form.Assign.value, this.form.ResolvedDate.value);'>

</form>
</html>

--------------------------------------------------------------------------------

Correct Calculation:
Assign= 12/29/2005 10:07
Resolved= 12/31/2005 10:18
Duration= 2:0:11

Incorrect Calculation:
Assign= 12/29/2005 09:07
Resolved= 12/31/2005 10:18
Duration= 2:0:-1369

Incorrect Calculation
Assign= 12/31/2005 10:07
Resolved= 12/31/2005 10:18
Duration= 0:2:-2749

Any ideas on how to modify my function to get the time calculation correct everytime?
 
Try this:

Code:
<HTML>
<HEAD>
<TITLE>Ticket Tracker</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function ResolvedDuration(date1, date2) {
    var date1 = new Date(date1);
    var date2 = new Date(date2);
    
    var theDifference = date2 - date1;
    
    var days = Math.floor( theDifference / (1000 * 60 * 60 * 24) );
    theDifference -= (days * 1000 * 60 * 60 * 24);
    
    var hours = Math.floor( theDifference / (1000 * 60 * 60) );
    theDifference -= (hours * 1000 * 60 * 60);
    
    var mins = Math.floor( theDifference / (1000 * 60) );
    
    document.forms[0].Resolved_Duration.value = days + ":" + hours + ":" + mins;
}
//--></script>
</HEAD>

<form method="post" action="ticketform.asp">
Assigned:
<input type="text" name="Assign" SIZE="20" value="">
<br>
Resolved:
<input type="text" name="ResolvedDate" SIZE="20" value="">
<br>
Duration:

<input type="text" name="Resolved_Duration" SIZE="20" value="" onBlur='ResolvedDuration(this.form.Assign.value, this.form.ResolvedDate.value);'>

</form>
</html>

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
That looks like it is working much better. Now if I wanted to update the duration box only

if both dates were in the following format mm/dd/yyyy hh:mm what would I need to add? if this format validation is difficult, what would I need to add to only update the box if the values are both not blank?

Thanks, I am new to javascript so your help is appreciated
 
I don't get your first question. Your second question is easy.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
if both dates were in the following format mm/dd/yyyy hh:mm


Basically, I want to add a validation on both boxes when they are entered to ensure that the data is in the format of mm/dd/yyyy hh:mm

Then when I call the duration function, I want to make sure both values are not blank before doing the calculation.

Can I pass as a parameter the name of the text box I want to update? IE, I want to call this function from multiple boxes.


Lastly, I came up with an error when executing your function.

Date1 10/05/2004 12:21
Date2 12/20/2004 12:21

Duration 76:1:0

Where does the extra hour come from?
 
date validation is an art in itself. take a look through the FAQ section of this forum. As for the error, I'm still investigating it and will get back to you.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Thanks for the reply, I will read the FAQ on date validation while I eat some lunch.

Thanks
 
Here is your function with [tt]basic[/tt] form validation (won't do calculations if dates are blank). Also, I've added a parameter so you can pass the field in.

Code:
function ResolvedDuration(date1, date2, targ) {
    if (date1 != '' && date2 != '') {
        var date1 = new Date(date1);
        var date2 = new Date(date2);
        
        var theDifference = date2 - date1;
        alert("ms: " + theDifference);
        
        var days = Math.floor( theDifference / daysInMs );
        theDifference -= (days * daysInMs);
        alert("d: " + days);
        
        var hours = Math.floor( theDifference / hoursInMs );
        theDifference -= (hours * hoursInMs);
        alert("hs: " + hours);
        
        var mins = Math.floor( theDifference / minsInMs );
        alert("min: " + mins);
        
        targ.value = days + ":" + hours + ":" + mins;
    }
}
Here is how you'd call it:
Code:
<input type="text" name="Resolved_Duration" SIZE="20" value="" onblur="ResolvedDuration(this.form.Assign.value, this.form.ResolvedDate.value, this.form.Resolved_Duration);">

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
sorry, you can get rid of those alerts if you'd like. i put them in there to debug.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
I now get an error daysInMs is not defined.

Will this function solve the extra hour issue I was having?

I see how you added the validation for blanks; thanks
 
sorry about that:

Code:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin

var daysInMs = 1000 * 60 * 60 * 24;
var hoursInMs = 1000 * 60 * 60;
var minsInMs = 1000 * 60;
    
function ResolvedDuration(date1, date2, targ) {
    if (date1 != '' && date2 != '') {
        var date1 = new Date(date1);
        var date2 = new Date(date2);
        
        var theDifference = date2 - date1;
        alert("ms: " + theDifference);
        
        var days = Math.floor( theDifference / daysInMs );
        theDifference -= (days * daysInMs);
        alert("d: " + days);
        
        var hours = Math.floor( theDifference / hoursInMs );
        theDifference -= (hours * hoursInMs);
        alert("hs: " + hours);
        
        var mins = Math.floor( theDifference / minsInMs );
        alert("min: " + mins);
        
        targ.value = days + ":" + hours + ":" + mins;
    }
}
//--></script>

no, it won't solve the extra hour. i have a feeling it has to do with javascript rounding.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Rouding error huh? that sucks :) I guess I have to tell my client to make sure they close all their tickets within 1 month :)
 
hold on a few minutes, i have come to a baffling understanding.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Your answer: Daylight Saving Time.
It occurred in 2004 on October 31st, attributing to the weird hour problem.

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
Are you serious, the calculation automatically takes into account DST? I was thinking that was it but I kept thinking I was wrong.

I wonder if I should exclude this extra-hour, I should reach out to my end user and ask him :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top