Problem with difference hour

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viki1967
    Contributor
    • Oct 2007
    • 263

    Problem with difference hour

    Hi all!

    This new forum its great! :)

    Congratulations !!!

    My answer: why this my code not working?
    Nothing error but not work the difference.... :

    Code:
    <html>
    
    <head>
    <script language="JavaScript"> 
    <!-- 
    
    
    var difference = function(leftHour, rightHour){
    	if(!leftHour || !rightHour)
    		return null;
    	spt = leftHour.split(":");
    	var totLeft = parseInt(spt[0]) * 60 + parseInt(spt[1]);
    	spt = rightHour.split(":");
    	var totRight = parseInt(spt[0]) * 60 + parseInt(spt[1]);
    	return totLeft - totRight;
    }
    
    var  diff1 = difference(myForm.F.value, myForm.E.value);
    var  diff2 = difference(myForm.D.value, myForm.A.value);
    var  diff3 = difference(myForm.G.value, myForm.F.value);
    var  diff4 = difference(myForm.H.value, myForm.G.value);
    var  diff5 = difference(myForm.H.value, myForm.A.value);
    
    // -->
    </script>
    
    </head>
    <body>
    
    <form name="myForm">
    
    <input type="text" name="A" size="20">
    <input type="text" name="B" size="20">
    <input type="text" name="C" size="20">
    <input type="text" name="D" size="20">
    <input type="text" name="E" size="20">
    <input type="text" name="F" size="20">
    <input type="text" name="G" size="20">
    <input type="text" name="H" size="20">
      
    <input type="text" name="RISULTATO1" size="20">
    <input type="text" name="RISULTATO2" size="20">
    <input type="text" name="RISULTATO3" size="20">
    <input type="text" name="RISULTATO4" size="20">
    <input type="text" name="RISULTATO5" size="20">
    
    
    </form>
    
    </body>
    
    </html>
  • viki1967
    Contributor
    • Oct 2007
    • 263

    #2
    New code:

    Code:
    <html>
    
    <head>
    <script language="JavaScript"> 
    <!-- 
    
    
    var difference = function(leftHour, rightHour){
    	if(!leftHour || !rightHour)
    		return null;
    	spt = leftHour.split(":");
    	var totLeft = parseInt(spt[0]) * 60 + parseInt(spt[1]);
    	spt = rightHour.split(":");
    	var totRight = parseInt(spt[0]) * 60 + parseInt(spt[1]);
    	return totLeft - totRight;
    }
    
    function Calcola()
    {
    
    	var diff1 = difference(myForm.F.value, myForm.E.value);
    	var diff2 = difference(myForm.D.value, myForm.A.value);
    	var diff3 = difference(myForm.G.value, myForm.F.value);
    	var diff4 = difference(myForm.H.value, myForm.G.value);
    	var diff5 = difference(myForm.H.value, myForm.A.value);
    	
    	myForm.RISULTATO1.value = diff1
        myForm.RISULTATO2.value = diff2
        myForm.RISULTATO3.value = diff3
        myForm.RISULTATO4.value = diff4
        myForm.RISULTATO5.value = diff5
        
    
    }
    // -->
    </script>
    
    </head>
    <body>
    
    <form name="myForm">
    
    A<input type="text" name="A" size="20">
    B<input type="text" name="B" size="20">
    C<input type="text" name="C" size="20">
    D<input type="text" name="D" size="20">
    E<input type="text" name="E" size="20">
    F<input type="text" name="F" size="20">
    G<input type="text" name="G" size="20">
    H<input type="text" name="H" size="20">
    
    <br><br>
      
    F-E<input type="text" name="RISULTATO1" size="20" onClick="Calcola()">
    D-A<input type="text" name="RISULTATO2" size="20" onClick="Calcola()">
    G-F<input type="text" name="RISULTATO3" size="20" onClick="Calcola()">
    H-G<input type="text" name="RISULTATO4" size="20" onClick="Calcola()">
    H-A<input type="text" name="RISULTATO5" size="20" onClick="Calcola()">
    
    
    </form>
    
    </body>
    
    </html>

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      That's an improvement. In place of myform, use document.myform .

      You may also want to use one button only to call the function rather than onclick of each text box.

      Comment

      • viki1967
        Contributor
        • Oct 2007
        • 263

        #4
        many thanks x your reply.

        this script now working but I need check that:
        • hour field B ever > hour field A
        • hour field C ever > hour field B
        • hour field D ever > hour field C
        • hour field E ever > hour field D
        • hour field F ever > hour field E
        • hour field G ever > hour field F
        • hour field H ever > hour field G

        Can you help me ?

        My script:

        Code:
        <html>
        
        <head>
        <script language="JavaScript"> 
        <!-- 
        
        
        var difference = function(leftHour, rightHour){
        	if(!leftHour || !rightHour)
        		return 0;
        	spt = leftHour.split(":");
        	var totLeft = parseInt(spt[0],10) * 60 + parseInt(spt[1],10);
        	spt = rightHour.split(":");
        	var totRight = parseInt(spt[0],10) * 60 + parseInt(spt[1],10);
        	
        	if (totLeft < totRight) {
        
            alert("ko");
            return 0;
        
        }
        	
        	return totLeft - totRight;
        }
        
        function Calcola()
        {
        
        	var diff1 = difference(document.myForm.F.value, document.myForm.E.value);
        	var diff2 = difference(document.myForm.D.value, document.myForm.A.value);
        	var diff3 = difference(document.myForm.G.value, document.myForm.F.value);
        	var diff4 = difference(document.myForm.H.value, document.myForm.G.value);
        	var diff5 = difference(document.myForm.H.value, document.myForm.A.value);
        	
        	document.myForm.RISULTATO1.value = diff1
        	document.myForm.RISULTATO2.value = diff2
        	document.myForm.RISULTATO3.value = diff3
        	document.myForm.RISULTATO4.value = diff4
        	document.myForm.RISULTATO5.value = diff5
            
        
        }
        // -->
        </script>
        
        </head>
        <body>
        
        <form name="myForm">
        
        A<input type="text" name="A" size="20" onkeyup="Calcola()">
        B<input type="text" name="B" size="20" onkeyup="Calcola()">
        C<input type="text" name="C" size="20" onkeyup="Calcola()">
        D<input type="text" name="D" size="20" onkeyup="Calcola()">
        E<input type="text" name="E" size="20" onkeyup="Calcola()">
        F<input type="text" name="F" size="20" onkeyup="Calcola()">
        G<input type="text" name="G" size="20" onkeyup="Calcola()">
        H<input type="text" name="H" size="20" onkeyup="Calcola()">
        
        <br><br>
          
        F-E<input type="text" name="RISULTATO1" size="20" value="0" readonly>
        D-A<input type="text" name="RISULTATO2" size="20" value="0" readonly>
        G-F<input type="text" name="RISULTATO3" size="20" value="0" readonly>
        H-G<input type="text" name="RISULTATO4" size="20" value="0" readonly>
        H-A<input type="text" name="RISULTATO5" size="20" value="0" readonly>
        
        
        </form>
        
        </body>
        
        </html>

        Comment

        • viki1967
          Contributor
          • Oct 2007
          • 263

          #5
          Any suggestions ?
          Viki

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Do you want to make this generic (i.e. for any number of fields) or will it always be 8 fields from A to H?

            Comment

            • viki1967
              Contributor
              • Oct 2007
              • 263

              #7
              Try this... working... :)

              Code:
              <script> 
              
              function isTime(str) { 
              	str="0"+str+":0"
              	spz = str.replace("\.",":").split(":"); // accetta anche il . come separatore
              	hh = parseInt(spz[0],10)
              	mm = parseInt(spz[1],10)
              	if ((hh == 24 && mm == 0) || (hh < 24 && mm < 60)) 
              		return (hh*60+mm);
              	return false;
              } 
              
              function Calcola() {
               for(var i=1; i<=7; i++) {
              	cx = ("ABCDEFGH").substr(i-1,1);
              	cy = ("ABCDEFGH").substr(i,1);
              	x = isTime(document.myForm.elements(cx).value);
              	y = isTime(document.myForm.elements(cy).value);
              	if(x && y) {
              		document.myForm.elements(cy).style.background='white';
              		if(y>x) {
              			document.myForm.elements("RISULTATO"+i).value = (y-x);
              		} else {
              			document.myForm.elements(cy).style.background = 'orange';
              		}
              	}
               }
              }
              </script>
              
              <form name="myForm">
              
              A<input type="text" name="A" size="5" onkeyup="Calcola()">
              B<input type="text" name="B" size="5" onkeyup="Calcola()">
              C<input type="text" name="C" size="5" onkeyup="Calcola()">
              D<input type="text" name="D" size="5" onkeyup="Calcola()">
              E<input type="text" name="E" size="5" onkeyup="Calcola()">
              F<input type="text" name="F" size="5" onkeyup="Calcola()">
              G<input type="text" name="G" size="5" onkeyup="Calcola()">
              H<input type="text" name="H" size="5" onkeyup="Calcola()">
              
              <br><br>
                
              B-A<input type="text" name="RISULTATO1" size="5" value="0" readonly>
              C-B<input type="text" name="RISULTATO2" size="5" value="0" readonly>
              D-C<input type="text" name="RISULTATO3" size="5" value="0" readonly>
              E-D<input type="text" name="RISULTATO4" size="5" value="0" readonly>
              F-E<input type="text" name="RISULTATO5" size="5" value="0" readonly>
              G-F<input type="text" name="RISULTATO6" size="5" value="0" readonly>
              H-G<input type="text" name="RISULTATO7" size="5" value="0" readonly>
              
              </form>

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Nice - thanks for posting!

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Sorry for nit-picking; I'm bored.

                  Your scripts should be done like so:

                  Code:
                  <script type="text/javascript">
                  
                  [...]
                  
                  </script>
                  :D

                  Comment

                  • viki1967
                    Contributor
                    • Oct 2007
                    • 263

                    #10
                    Originally posted by Markus
                    Sorry for nit-picking; I'm bored.

                    Your scripts should be done like so:

                    Code:
                    <script type="text/javascript">
                    
                    [...]
                    
                    </script>
                    :D
                    Many thanks !!! :-)
                    viki

                    Comment

                    Working...