Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    decimal time problem

    I'm trying to work out the different between two times in decimal.... but its not working...

    eg: start time = 10.00
    end time = 11.00

    the difference should be 1.00, but its coming out as 1.10

    starttime = 13.30
    end time = 15.00

    it should be 1.5 hours difference, yet it comes up with 1.15!

    you can see it in action here: http://www.mattfacer.com/test.htm

    here's the entire code

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="javascript">
    function calculateHours() {
    	
    	//work out the fractional times
    	message = "";
    	
    	if (document.frm.endTime.value == "")   { endTime = "0.00";   } else { endTime = document.frm.endTime.value;     } 
    	if (document.frm.startTime.value == "") { startTime = "0.00"; } else { startTime = document.frm.startTime.value; } 
    	
    	
    	if ((document.frm.endTime.value == "") || (document.frm.endTime.value == "")) {
        	message = "Could not calculate - Both start and end times must be entered";
      	}
    	
    	//times
    	if (document.frm.startTime.value > document.frm.endTime.value) {    
    		message = "Could not calculate - The start time cannot be greater than the end time";   
    	}
    	
    	
    	var startArray = startTime.split(".");
    	var endArray = endTime.split(".");
    	
    	//minutes
    	var startminutes = startArray[0]*60 + startArray[1]*1;
    	
    	var endminutes   = endArray[0]*60   + endArray[1]*1;
    	var totalminutes = endminutes - startminutes;
    	var fractionMinutes = (totalminutes/60)*100;
    	
    	var totalhours   = Math.floor(totalminutes / 60);
    	var totalminutes = totalminutes%60;
    	var fractionalTime = totalhours + "." + fractionMinutes;
    	
    	message = "Your hours are: " + parseFloat(fractionalTime).toFixed(2);
    	
    	alert(message);	
    
    }
    function validate(ctrl,whichField) {
    	if (!isNumeric(ctrl.value,ctrl)) {
    		alert("Please enter only numbers in this field");
    		ctrl.focus();
    		ctrl.value = "";
    		event.cancelBubble = true;
    		return false;
    	}
    }
    
    function roundIt(field,time)
     {
    	var newVal = parseFloat(field.value).toFixed(2);
    	if ((newVal < 10) && (time != "")) {
    		field.value = "0" + newVal;
    	} else {
    		field.value = newVal;
    	}
    	
     }
    
    </script>
    </head>
    
    <body>
    <form name="frm">
    <table width="100%"  border="0" cellpadding="3" cellspacing="0">
      <tr>
        <td align="right">Start Time:</td>
        <td><span class="row">
          <input name="startTime" type="text" id="startTime" onChange="roundIt(this,'yes'); return validate(this,'startTime');">
        </span></td>
      </tr>
      <tr>
        <td align="right">End Time: </td>
        <td><span class="row">
          <input name="endTime" type="text" id="endTime" onChange="roundIt(this,'yes'); return validate(this,'endTime');">
        </span></td>
      </tr>
      <tr>
        <td align="right">&nbsp;</td>
        <td><a href="javascript:calculateHours()">calculate</a></td>
      </tr>
    </table>
    </form>
    </body>
    </html>
    any help will be much appreciated!!!!

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The first problem i see is that you call a function 'isNumeric' and that funciton does not exist.

    i'll get this working and re-post.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    this is a cut down version of a much larger page - so i may have missed out some of the functions... .sorry!

    here's the isNumeric function

    Code:
    function isNumeric(string, field, wipeField) {
    		re = /^\d+(\.\d+)?$/;
    		if (re.test(string)) {
    			return true;
    		} else {
    			return false;
    		}
    }
    Cheers

  • #4
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i used some concepts you had but i found it easier to just re-write the page. here it is working for me in FF. i havn't tested it for bugs fully yet so bare with me. as far as i can tell however there shouldn't be any problems with it.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript" type="text/javascript">
    	function validate(id){
    		var e = document.getElementById(id);
    		var cv = e.value;
    		if(!cv.match("^[0-9]{1,2}[/:][0-9]{2}")){
    			// checks
    			var vfront = false; var vback = false;
    			if(cv.match("^[0-9]{1,2}")){ vfront = true; } // front
    			if(cv.match("[0-9]{2}$")){ vback = true; }
    			var output = '';
    			if(vfront == true){ output+=cv.match("^[0-9]{1,2}"); } else { output+='00'; }
    			output+=':';
    			if(vback == true){ output+=cv.match("[0-9]{2}$"); } else { output+='00'; }
    			e.value = output; // rebuild output
    		}	
    	}
    	function calculateTime(id1,id2){ /* id2.value - id1.value */
    		var e1 = document.getElementById(id1); var e2 = document.getElementById(id2);
    		var e1_minutes = toMins(id1);
    		var e2_minutes = toMins(id2);
    		var outMins = e2_minutes - e1_minutes;
    		var total = fromMins(outMins);
    		var spOUT = document.getElementById('output');
    		spOUT.innerHTML = "The total time: "+total;
    		
    	}
    	function toMins(id){
    		var e = document.getElementById(id);
    		var eA = e.value.split(':');
    		var eAM = Math.floor( parseInt(eA[0] * 60) + parseInt(eA[1]) );
    		return eAM;
    	}
    	function fromMins(mins){
    		hours = Math.floor(mins/60);
    		minutes = mins - (hours*60);
    		time = hours + ":" + minutes;
    		return time;
    	}
    </script>
    </head>
    <body>
    <table cellpadding="0" cellspacing="0" border="0">
    	<tr>
    		<td>StartTime:</td>
    		<td>
    			<input type="text" name="startTime" id="startTime" onChange="validate(this.id)" />
    		</td>
    	</tr>
    	<tr>
    		<td>EndTime:</td>
    		<td>
    			<input type="text" name="endTime" id="endTime" onChange="validate(this.id)" />
    		</td>
    	</tr>
    	<tr><td colspan="2"><input type="button" onClick="calculateTime('startTime','endTime')" value="calculate" /></td></tr>
    	<tr><td colspan="2"><span id="output"></span></td></tr>
    </table>
    </body>
    </html>
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #5
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    946
    Thanks
    0
    Thanked 129 Times in 128 Posts
    var totalhours = Math.floor(totalminutes / 60);
    var totalminutes = totalminutes%60;
    var fractionalTime = totalhours + "." + fractionMinutes;
    these fractionMinutes must be totalminutes

  • #6
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    thanks for the replies...

    I tried hte first suggestion - that works VERY nicely for working out the hours and minutes... but the thing is, I need this as a decimal.. .eg: 1 hour 45 minutes should be 1.75 hours

    I tried changing the original code as lerura suggested... but that just shows the minutes.... so 2 hours 30 comes out as 2.30 when it should be 2.5

    thanks very much for your help!

  • #7
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    946
    Thanks
    0
    Thanked 129 Times in 128 Posts
    then change the line to:

    Code:
     var fractionalTime = totalhours + "." + totalminutes/0.6;

  • #8
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    thanks lerura - thats working perfectly now!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •