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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    working out the decimal time

    hey guys,

    I have an application where a user enters a start time, and an end time in 24 hours format, eg:

    startTime = 14.00
    endTime = 15.30

    Using the function below, I can work out that they've worked for 1.5 hours. the problem is, if you look at the code, its fairly basic maths..... so when they enter

    startTime = 12.30
    endTime = 13.00

    the calculation says they've done 1.5 hours!!
    Is there a better way to do this, or is there a way I can work out against a time function ....

    Code:
    function calculateHours() {
    	
    	//work out the fractional times
    	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";
    	} else {
    		//times
    		if (document.frm.startTime.value > document.frm.endTime.value) {
    			
    			message = "Could not calculate - The start time cannot be greater than the end time";
    			
    		} else {
    			
    			var startArray = startTime.split(".");
    			var endArray = endTime.split(".");
    			
    			var totalHours = endArray[0] - startArray[0];
    			
    			
    			if (parseInt(endArray[1]) > parseInt(startArray[1])) { 
    				var totalMinutes = parseInt(endArray[1]) - parseInt(startArray[1]);
    			}
    			
    			if (parseInt(endArray[1]) == parseInt(startArray[1])) { 
    				var totalMinutes = 0;
    			}
    			
    			if (parseInt(endArray[1]) < parseInt(startArray[1])) { 
    				var totalMinutes = (60-parseInt(startArray[1])) + parseInt(endArray[1]);
    			}
    			
    			//now we have the minutes and the hours, we can work out the fraction
    			var fraction = (totalMinutes / 60)*100;
    			var fractionalTime = totalHours + "." + fraction;
    			
    			
    			message = "Your hours are: " + parseFloat(fractionalTime).toFixed(2);
    			
    		}
    	}
    	
    	alert(message);
    	
    	
    }
    thanks for any help

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Specify the optional radix parameter of parseInt(string, [radix]) method. Specify 10 as the base. If not specified, you may get unexpected results if the string starts at 0 which will be parsed as an octal number. I'm not sure though if this causes the wrong computation. Try fixing that and we'll see.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    thanks.... but not sure how that will help me? Some times maybe 08.00 etc...

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    632
    Thanks
    1
    Thanked 21 Times in 21 Posts
    Code:
    function calculateHours() {
      if ((document.frm.endTime.value == "") 
      || (document.frm.endTime.value == "")) {
        message = "Could not calculate - Both start and end times must be entered";
        return false;
      }
    
      //times
      if (document.frm.startTime.value > document.frm.endTime.value) {    
        message = "Could not calculate - The start time cannot be greater than the end time";
        return false;    
      }
        
      var startArray = startTime.split(".");
      var endArray = endTime.split(".");
      
      var startminutes = startArray[0]*60 + startArray[1]*1;
      var endminutes   = endArray[0]*60   + endArray[1]*1;
      var totalminutes = endminutes - startminutes;
      
      var totalhours   = Math.floor(totalminutes / 60);
      var totalminutes = totalminutes%60;
      var fractionalTime = totalHours + "." + fraction;
      
      message = "Your hours are: " + parseFloat(fractionalTime).toFixed(2);
        
      alert(message);
        
    }

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    cheers..that seems to work ok(Ish)... there's a problem though for example if you enter

    start = 12.30
    end = 13.45

    it says the answer is 1.13 when it should be 1.25

    your code seemed to not convert the minutes in to a fraction... eg: start = 12.30 end = 13.00 produced "0.30" when it should be 0.50... I added:

    fractionMinutes = (totalminutes/60)*100;

    which does the job... so the code now reads:

    Code:
    	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);
    how can I get it to do the first example, 1 hour and 15 minutes.... ?!
    thanks.


  •  

    Posting Permissions

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