Link to home
Start Free TrialLog in
Avatar of bagni99
bagni99

asked on

Calculate Time Difference in Minutes -then divide by time unit

Hi All,

I have 5 fields:

START TIME          END TIME           TOTAL TIME      TIME UNIT          TOTAL UNITS

What I need is to calculate the difference in time (in minutes) between the START TIME and the END TIME.  Then divide the minutes by the TIME UNIT (which default is 15 but can be changed).  The result is then to be put in the TOTAL UNITS field.

So if
START TIME          END TIME           TOTAL TIME      TIME UNIT          TOTAL UNITS
10:00                    11:00                 60                    15                      4

I don't really need the TOTAL TIME field and would like to discard it if possible.  The TIME UNIT will probably be a hidden field.

I currently have been using the following Javascript to calculate the time in hours and minutes.

<script language=javascript>
          function calculate(){
               var start = document.getElementById("x_os_starttime").value.split(':');
               var end = document.getElementById("x_os_endtime").value.split(':');
               var total = document.getElementById("x_os_totaltime");

               var shour = parseInt(start[0].substring(0,1)== '0'? start[0].substring(1):start[0]);
               var smin = parseInt(start[1]);
               var ehour = parseInt(end[0].substring(0,1)== '0'? end[0].substring(1):end[0]);
               var emin = parseInt(end[1]);
               
               if (shour>ehour)
                    alert("error");
               else if (shour == ehour)
                    total.value = "00:"+(emin-smin);
               else{
                    if (emin>=smin)
                         total.value=(ehour-shour)+":"+(emin-smin);
                    else
                         total.value=(ehour-shour-1)+":"+(60-smin+emin);
               }
          }
          </script>

The function is called by an    onChange="calculate()"     which is attached to the END TIME field.

I am not sure how to change the code to calculate in minutes then divide by the TIME UNIT.  Can anyone help me?  
I did find that someone had asked the 'minute calculation' question before but the answer was a dead link to another website.

Better code ideas are welcomed.

Bagni99
ASKER CERTIFIED SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bagni99
bagni99

ASKER

Thanks Zyloch!
I think we are getting really close.
I have tried the code and there seems to be one little glich.  

If I use the example START TIME 10: 00,  END TIME 11:00. The value returned in the TOTAL UNITS field is 60 (which is the minutes) instead of 4 (the total time units).  I have tried entering different times and the value is clearly the minutes calculated...which is great, but now I also need the minutes divided by the TIME UNIT.  Do you know what we need to do????

bagni99
I have tried it with 10:00 and 11:00 and it outputs 4 as expected. The time units is 15, set in a hidden form field that you can change. I've tried in both FF2 and IE7, and it should work in previous versions as well.
Avatar of bagni99

ASKER

Thanks again.
Thanks for your patience.
You have done exactly what I have asked.

I know what the problem is....it's to do with something I failed to tell you.

The TIME UNITS field (that can be changed) is actually a combo box.  So the default is 15 but it's value is 1.  Figure that is why the time units equals the same as the minutes.

I'll double the points if you can tell me if there is a way for you to use the number in the combo box as opposed to the value.

Thanks again,
bagni99
You only need to change

    var units = Math.floor(minutes / parseInt(frm.x_os_timeunits.value));

to

    var timeunits = parseInt(frm.x_os_timeunits.options[frm.x_os_timeunits.selectedIndex].value);
    var units = Math.floor(minutes / timeunits);
Avatar of bagni99

ASKER

I've just tried changing the code - I haven't got it working yet.  I don't have the time to work out what I have done wrong right now....I'll have a look later today.  Thanks again, I'll get back to you soon.
You have to make sure that your <select> dropdown is in between the <form> tags, and that its name is x_os_timeunits
Avatar of bagni99

ASKER

Sorry for the delay.

I've been flat out with work and the damn internet has been playing up (I am in East Timor at the moment).

I have got the code working but it seems to still be using the value (1) not the number that is entered (15).  The client requires the option to change the number as required..maybe to 20 time units or 30.  The value probably wont ever change from 1.

Unsure where to go from here.  I'll keep trying other things.



What is your current code?
Avatar of bagni99

ASKER

<script type="text/javascript">
<!--

function calculate(frm) {
    var start = new Date();
    var end = new Date();

    //create the start and end dates
    var startPieces = frm.x_os_starttime.value.split(':');
    var endPieces = frm.x_os_endtime.value.split(':');

    start.setHours(parseInt(startPieces[0]), parseInt(startPieces[1]) % 60);
    end.setHours(parseInt(endPieces[0]), parseInt(endPieces[1]) % 60);

    if (start > end) {
        alert('Start time must less than end time.');
        return false;
    }

    var difference = end - start;
    var minutes = difference / 60000;

    var timeunits = parseInt(frm.x_os_timeunits.options[frm.x_os_timeunits.selectedIndex].value);
    var units = Math.floor(minutes / timeunits);

    frm.x_os_totalunits.value = units;
}

function pad(txt, ch, sz, dir) {
    if (arguments.length < 4) {
        dir = 'left'
    }

    txt = txt.toString(); ch = ch.toString();
    var numchars = Math.floor((sz - txt.length) / ch.length);
    if (dir == 'left') {
        for (var i = 0; i < numchars; i++) {
            txt = ch + txt;
        }
    } else {
        for (var i = 0; i < numchars; i++) {
            txt += ch;
        }
    }

    return txt;
}

//-->
</script>

.............

<form name="foccasion_of_serviceadd" id="foccasion_of_serviceadd" action="occasion_of_serviceadd.asp" method="post" onSubmit="return EW_checkMyForm(this);">

..............      

<tr>
            <td class="ewTableHeader"><span>Start Time</span></td>
            <td class="ewTableAltRow"><span id="cb_x_os_starttime">
<input type="text" name="x_os_starttime" id="x_os_starttime" size="12" value="<% If IsDate(x_os_starttime) Then Response.Write EW_FormatDateTime(x_os_starttime,4) Else Response.Write x_os_starttime End If %>">
</span></td>
      </tr>
      <tr>
            <td class="ewTableHeader"><span>End Time</span></td>
            <td class="ewTableAltRow"><span id="cb_x_os_endtime">
<input type="text" name="x_os_endtime" id="x_os_endtime" size="12" value="<% If IsDate(x_os_endtime) Then Response.Write EW_FormatDateTime(x_os_endtime,4) Else Response.Write x_os_endtime End If %>">
</span></td>
      </tr>
      <tr>
            <td class="ewTableHeader"><span>Total Units</span></td>
            <td class="ewTableAltRow"><span id="cb_x_os_totalunits">
<% If IsNull(x_os_totalunits) or x_os_totalunits = "" Then x_os_totalunits = 0 ' Set default value %>
<input type="text" name="x_os_totalunits" id="x_os_totalunits" size="5" value="<%= Server.HTMLEncode(x_os_totalunits&"") %>">
</span><input type="button" value="Calculate" onclick="calculate(this.form);"></td>
      </tr>
      <tr>
            <td class="ewTableHeader"><span>os timeunits</span></td>
            <td class="ewTableAltRow"><span id="cb_x_os_timeunits">
<% If IsNull(x_os_timeunits) or x_os_timeunits = "" Then x_os_timeunits = 1 ' Set default value %>
<%
lst_x_os_timeunits = "<select id='x_os_timeunits' name='x_os_timeunits'>"
lst_x_os_timeunits = lst_x_os_timeunits & "<option value=''>Please Select</option>"
sSqlWrk = "SELECT [timeunit_id], [timeunit_value] FROM [timeunit]"
If x_os_timeunits = "" Or IsNull(x_os_timeunits) Then
      sSqlWrk = sSqlWrk & " WHERE 0=1"
Else
      sSqlWrk = sSqlWrk & " WHERE [timeunit_id] = " & AdjustSql(x_os_timeunits) & ""
End If
Set rswrk = Server.CreateObject("ADODB.Recordset")
rswrk.Open sSqlWrk, conn
If Not rswrk.Eof Then
      datawrk = rswrk.GetRows
      rowswrk = UBound(datawrk, 2)
      For rowcntwrk = 0 To rowswrk
            lst_x_os_timeunits = lst_x_os_timeunits & "<option value='" & datawrk(0, rowcntwrk) & "'"
            If CStr(datawrk(0, rowcntwrk)&"") = CStr(x_os_timeunits&"") Then
                  lst_x_os_timeunits = lst_x_os_timeunits & " selected"
            End If
            lst_x_os_timeunits = lst_x_os_timeunits & ">" & datawrk(1, rowcntwrk) & "</option>"
      Next
End If
rswrk.Close
Set rswrk = Nothing
lst_x_os_timeunits = lst_x_os_timeunits & "</select>"
Response.Write lst_x_os_timeunits
sSqlWrk = "SELECT [timeunit_id], [timeunit_value] FROM [timeunit]"
sSqlWrk = EW_Encode(TEAencrypt(sSqlWrk, EW_RANDOM_KEY))
%>
<input type="hidden" name="s_x_os_timeunits" value="<%= sSqlWrk %>">
<input type="hidden" name="lc_x_os_timeunits" value="2">
<input type="hidden" name="ld1_x_os_timeunits" value="1">
<input type="hidden" name="ld2_x_os_timeunits" value="-1">
</span><input type="button" value="Calculate" onclick="calculate(this.form);"></td>
      </tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="ADD">
</form>
Is it possible to only get the form from the generated HTML (browser View Source)? I don't see anything wrong taking a look at this code--it seems perfectly fine, but the generated code might provide clues easier to discern.
Avatar of bagni99

ASKER

Here's the whole form. Thanx



<form name="foccasion_of_serviceadd" id="foccasion_of_serviceadd" action="occasion_of_serviceadd.asp" method="post" onSubmit="return EW_checkMyForm(this);">
<p>
<input type="hidden" name="a_add" value="A">

<table class="ewTable">
      <tr>
            <td class="ewTableHeader"><span>Date</span></td>
            <td class="ewTableAltRow"><span id="cb_x_os_date">
<input type="text" name="x_os_date" id="x_os_date" size="12" value="">
&nbsp;<img src="images/ew_calendar.gif" id="cx_os_date" alt="Pick a date" style="cursor:pointer;cursor:hand;">
<script type="text/javascript">
Calendar.setup(
{
inputField : "x_os_date", // ID of the input field
ifFormat : "%d/%m/%Y", // the date format
button : "cx_os_date" // ID of the button
}
);
</script>
</span></td>
      </tr>
      <tr>
            <td class="ewTableHeader"><span>Health Professional</span></td>
            <td class="ewTableAltRow"><span id="cb_x_hp_id">
<select id='x_hp_id' name='x_hp_id'><option value=''>Please Select</option></select>
<input type="hidden" name="s_x_hp_id" value="B%1B%C3%99%2Fwo%C2%90!33!%C3%9C%2B%C2%B7X%C3%AEc%C2%B4VQ%06%C3%B6%C3%A2%17%C2%9D%C2%BC7%C2%9F%C3%B8u%C3%95%C2%BE%15(%C3%87%C2%96b%C3%B0%C3%AA%C3%A8%C2%84%C3%B8%C3%ADe%C3%89W%C3%B3%C3%AA%C3%A8t.akR%C3%8C%40%C2%94%1Do%C2%A9%C2%9F9C%C3%B6D%09%C2%9E%C3%92%C2%AC%C3%B4S%10%C2%A7%3C%C2%B8%C2%A3%C2%85%C2%BF%3A%C3%914%C2%93%24%C2%92%C2%AE%17%C3%92%C2%9D%2F%C3%8D%13%C2%AEV2%C3%A1%C3%A2%1CSN%05%23%3FW%C3%9C%60%C3%8E%C3%93h%C2%9F%C2%A8%C2%A4%C3%85%09u%3Dv%C3%AD%C3%A3-%C3%A8%09%C2%9B%C3%86U%C3%A9L%C2%A2">
<input type="hidden" name="lc_x_hp_id" value="3">
<input type="hidden" name="ld1_x_hp_id" value="1">
<input type="hidden" name="ld2_x_hp_id" value="2">
</span></td>
      </tr>
      <tr>
            <td class="ewTableHeader"><span>Venue</span></td>
            <td class="ewTableAltRow"><span id="cb_x_venue_id">
<select id='x_venue_id' name='x_venue_id'><option value=''>Please Select</option></select>
<input type="hidden" name="s_x_venue_id" value="%C3%B0!33!%C2%88%C3%8A%3A%C2%ADN%C3%B8xp%C2%9D%C3%B7%C3%8B%C2%96%C3%82)!0!%C3%A8V%17%C3%9DNnX%C3%B5%C2%B2v%1A%C3%99%C2%85%C2%85%2B.%C3%94f%C2%A0%22%C2%8Dc8%40%C3%B8k%7Bp%C2%9E%3CER%C2%84%C2%B1%C3%AD%C3%AC%C3%A9%C2%86%C3%A0f%C2%9E%C3%AA%C2%BC%C2%B7%601%C3%A8%C2%94%3D6k%C2%87%03%C2%BFt%20y%C2%A9%C2%A7%C2%AE%09Q%C3%9C%16%C3%983K%3E~%C3%BE%03%C2%9D%C3%97%C2%8D%C2%9A%C3%BB%C3%8EW4k%25%C2%9A%C3%94">
<input type="hidden" name="lc_x_venue_id" value="2">
<input type="hidden" name="ld1_x_venue_id" value="1">
<input type="hidden" name="ld2_x_venue_id" value="-1">
</span></td>
      </tr>
      <tr>
            <td class="ewTableHeader"><span>New or FUP</span></td>
            <td class="ewTableAltRow"><span id="cb_x_client_status">
<table class="aspmakerlist"><tr><td>
<input type="radio" name="x_client_status" checked value="1">
New</td><td>
<input type="radio" name="x_client_status" value="2">
FUP</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>
</span></td>
      </tr>
      <tr>
            <td class="ewTableHeader"><span>Client Type</span></td>
            <td class="ewTableAltRow"><span id="cb_x_os_clienttype">
<table class="aspmakerlist"><tr><td>
<input type="radio" name="x_os_clienttype" checked value="1">
Ind</td><td>
<input type="radio" name="x_os_clienttype" value="2">
Grp</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>
</span></td>
      </tr>
      <tr>
            <td class="ewTableHeader"><span>Client</span></td>
            <td class="ewTableAltRow"><span id="cb_x_client_id">
<select id='x_client_id' name='x_client_id'><option value=''>Please Select</option></select>
<input type="hidden" name="s_x_client_id" value="%C2%85%C3%8E%C2%93%C3%82%2F%C3%89%0F%24Q%05%C2%923j%C2%B4%C3%BE%C3%A9D%C3%8AJ%14%C3%99!33!%C3%80%C2%BAL1%C3%94%17%C2%86%C3%9B%C2%85%C3%BB%C3%A6%C2%A2%C3%B4%C3%BA!12!!13!%C3%BE%C2%B7%3A%C3%BEm%C3%99%25%22%C2%8Ae%C2%97%C2%B8%C3%B2%C3%B56%C3%A1N%C3%B1%C2%AD%C2%A8%20%C2%9F%C2%B4%15%C3%B8S%C2%A3D%20%C2%B0%1D%1Bg%C2%93F%C3%9C%03G%C3%9D%C3%85R2%C3%BB%C2%98%C3%A1%C2%8B%C3%B7%2CA%7F%C3%A4B%C3%96%C2%B6%C2%B8h(%08%C3%81-%C2%9B%25%C3%B0%C3%98!33!)%C3%83H%C2%A5%C3%92%01F%C3%BE4n%2C5%3Dbv%C3%B8%C3%82">
<input type="hidden" name="lc_x_client_id" value="3">
<input type="hidden" name="ld1_x_client_id" value="1">
<input type="hidden" name="ld2_x_client_id" value="2">
</span></td>
      </tr>
      <tr>
            <td class="ewTableHeader"><span>Group Client</span></td>
            <td class="ewTableAltRow"><span id="cb_x_grpclient_id">
<select id='x_grpclient_id' name='x_grpclient_id'><option value=''>Please Select</option></select>
<input type="hidden" name="s_x_grpclient_id" value="G%60%C2%AE*%C3%96%C2%B8%C3%9Bf%C3%A6%C3%95%C3%AF%C2%B6!12!8%1A%C3%A3K%C2%97%02!0!%08%1A.i%7F%C3%82%C3%9F%C2%8Az%C2%81%C2%9C%C2%93%C2%86hXm%C3%B9%C3%9A%1A%C2%9B%22%C2%AC%C2%86%1C%07%C2%BCE%5D%C3%86%C2%91%C2%9AJ%18%1A%C3%9BH'%C2%AAZ%C2%BA%C3%BC%C2%B0%C2%BE%C3%A4%3B%60%C3%B1%C2%99%20%C3%8E%C2%B1%C3%8D%0E%C3%93%C2%8E%16%C3%93%2F%C2%98%C2%BF%1E%C2%9D%01%C2%99w%C3%A4%C2%84%C2%A2%C3%91V%10%C2%B4%C3%96%C2%939%C2%8C%08%C2%B8%C2%BB%C3%90AZ%C2%9A%C3%B8%C2%AE%C3%8D9t">
<input type="hidden" name="lc_x_grpclient_id" value="2">
<input type="hidden" name="ld1_x_grpclient_id" value="1">
<input type="hidden" name="ld2_x_grpclient_id" value="-1">
</span></td>
      </tr>
      <tr>
            <td class="ewTableHeader"><span>Start Time</span></td>
            <td class="ewTableAltRow"><span id="cb_x_os_starttime">
<input type="text" name="x_os_starttime" id="x_os_starttime" size="12" value="">
</span></td>
      </tr>
      <tr>
            <td class="ewTableHeader"><span>End Time</span></td>
            <td class="ewTableAltRow"><span id="cb_x_os_endtime">
<input type="text" name="x_os_endtime" id="x_os_endtime" size="12" value="">
</span></td>
      </tr>
      <tr>
            <td class="ewTableHeader"><span>Total Units</span></td>
            <td class="ewTableAltRow"><span id="cb_x_os_totalunits">

<input type="text" name="x_os_totalunits" id="x_os_totalunits" size="5" value="0">
</span><input type="button" value="Calculate" onclick="calculate(this.form);"></td>
      </tr>
      <tr>
            <td class="ewTableHeader"><span>os timeunits</span></td>
            <td class="ewTableAltRow"><span id="cb_x_os_timeunits">
<select id='x_os_timeunits' name='x_os_timeunits'><option value=''>Please Select</option><option value='1' selected>15</option></select>
<input type="hidden" name="s_x_os_timeunits" value="0t%C3%A048%0EM%1B%0EM%C3%B8%C3%93%5C(%C2%9A%C3%9F%C3%84m!11!%10%C2%9As%3D%C2%96%3B%C3%8B%C2%9F%3D%C3%80.%C2%9E%C3%8B%1En%C2%99%2B(%C2%95%09%5B(w%C2%9A%C2%A5L%02%C2%A2%3C%C3%94%C3%BFb%C3%9A1%C2%BBl%C2%95%2BS%C3%8C%40zf%C2%B0%C2%9BZ%1Fp%C2%88">
<input type="hidden" name="lc_x_os_timeunits" value="2">
<input type="hidden" name="ld1_x_os_timeunits" value="1">
<input type="hidden" name="ld2_x_os_timeunits" value="-1">
</span><input type="button" value="Calculate" onclick="calculate(this.form);"></td>
      </tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="ADD">
</form>
This is because I used the value of the option. You have

    <option value='1' selected>15</option>

Either change it to

    <option value='15' selected>15</option>

or change my original code to

    var timeunits = parseInt(frm.x_os_timeunits.options[frm.x_os_timeunits.selectedIndex].text);
    var units = Math.floor(minutes / timeunits);
Avatar of bagni99

ASKER

Fantastic !!!

I used:

   var timeunits = parseInt(frm.x_os_timeunits.options[frm.x_os_timeunits.selectedIndex].text);
    var units = Math.floor(minutes / timeunits);

I promised to double your points but I have just read that I  can't award more than 500points.  I this true?  Do you know how if it is at all possible?  I wont PAQ this question just yet in case it is possible.  

Thanks again.

PS: I am going to post another question in a couple of hours asking how to round up the total time units.
No, you should not give more than 500 points. On this site, points are just an extra--it's helping people that counts :-D I won't be around in a couple of hours, but your problem is easily fixed

Round units down (current)

var units = Math.floor(minutes / parseInt(frm.x_os_timeunits.value));

Rounds units up

var units = Math.ceil(minutes / parseInt(frm.x_os_timeunits.value));

Round

var units = Math.round(minutes / parseInt(frm.x_os_timeunits.value));
Avatar of bagni99

ASKER

You are an absolute gem!!!

Thanks for all your help.  You are not only helping but you are teaching too.

bagni99 :)