469,632 Members | 1,730 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

Problem with difference hour

263 100+
Hi all!

This new forum its great! :)

Congratulations !!!

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

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <head>
  4. <script language="JavaScript"> 
  5. <!-- 
  6.  
  7.  
  8. var difference = function(leftHour, rightHour){
  9.     if(!leftHour || !rightHour)
  10.         return null;
  11.     spt = leftHour.split(":");
  12.     var totLeft = parseInt(spt[0]) * 60 + parseInt(spt[1]);
  13.     spt = rightHour.split(":");
  14.     var totRight = parseInt(spt[0]) * 60 + parseInt(spt[1]);
  15.     return totLeft - totRight;
  16. }
  17.  
  18. var  diff1 = difference(myForm.F.value, myForm.E.value);
  19. var  diff2 = difference(myForm.D.value, myForm.A.value);
  20. var  diff3 = difference(myForm.G.value, myForm.F.value);
  21. var  diff4 = difference(myForm.H.value, myForm.G.value);
  22. var  diff5 = difference(myForm.H.value, myForm.A.value);
  23.  
  24. // -->
  25. </script>
  26.  
  27. </head>
  28. <body>
  29.  
  30. <form name="myForm">
  31.  
  32. <input type="text" name="A" size="20">
  33. <input type="text" name="B" size="20">
  34. <input type="text" name="C" size="20">
  35. <input type="text" name="D" size="20">
  36. <input type="text" name="E" size="20">
  37. <input type="text" name="F" size="20">
  38. <input type="text" name="G" size="20">
  39. <input type="text" name="H" size="20">
  40.  
  41. <input type="text" name="RISULTATO1" size="20">
  42. <input type="text" name="RISULTATO2" size="20">
  43. <input type="text" name="RISULTATO3" size="20">
  44. <input type="text" name="RISULTATO4" size="20">
  45. <input type="text" name="RISULTATO5" size="20">
  46.  
  47.  
  48. </form>
  49.  
  50. </body>
  51.  
  52. </html>
Mar 6 '09 #1
9 2380
viki1967
263 100+
New code:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <head>
  4. <script language="JavaScript"> 
  5. <!-- 
  6.  
  7.  
  8. var difference = function(leftHour, rightHour){
  9.     if(!leftHour || !rightHour)
  10.         return null;
  11.     spt = leftHour.split(":");
  12.     var totLeft = parseInt(spt[0]) * 60 + parseInt(spt[1]);
  13.     spt = rightHour.split(":");
  14.     var totRight = parseInt(spt[0]) * 60 + parseInt(spt[1]);
  15.     return totLeft - totRight;
  16. }
  17.  
  18. function Calcola()
  19. {
  20.  
  21.     var diff1 = difference(myForm.F.value, myForm.E.value);
  22.     var diff2 = difference(myForm.D.value, myForm.A.value);
  23.     var diff3 = difference(myForm.G.value, myForm.F.value);
  24.     var diff4 = difference(myForm.H.value, myForm.G.value);
  25.     var diff5 = difference(myForm.H.value, myForm.A.value);
  26.  
  27.     myForm.RISULTATO1.value = diff1
  28.     myForm.RISULTATO2.value = diff2
  29.     myForm.RISULTATO3.value = diff3
  30.     myForm.RISULTATO4.value = diff4
  31.     myForm.RISULTATO5.value = diff5
  32.  
  33.  
  34. }
  35. // -->
  36. </script>
  37.  
  38. </head>
  39. <body>
  40.  
  41. <form name="myForm">
  42.  
  43. A<input type="text" name="A" size="20">
  44. B<input type="text" name="B" size="20">
  45. C<input type="text" name="C" size="20">
  46. D<input type="text" name="D" size="20">
  47. E<input type="text" name="E" size="20">
  48. F<input type="text" name="F" size="20">
  49. G<input type="text" name="G" size="20">
  50. H<input type="text" name="H" size="20">
  51.  
  52. <br><br>
  53.  
  54. F-E<input type="text" name="RISULTATO1" size="20" onClick="Calcola()">
  55. D-A<input type="text" name="RISULTATO2" size="20" onClick="Calcola()">
  56. G-F<input type="text" name="RISULTATO3" size="20" onClick="Calcola()">
  57. H-G<input type="text" name="RISULTATO4" size="20" onClick="Calcola()">
  58. H-A<input type="text" name="RISULTATO5" size="20" onClick="Calcola()">
  59.  
  60.  
  61. </form>
  62.  
  63. </body>
  64.  
  65. </html>
  66.  
Mar 6 '09 #2
acoder
16,027 Expert Mod 8TB
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.
Mar 7 '09 #3
viki1967
263 100+
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:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <head>
  4. <script language="JavaScript"> 
  5. <!-- 
  6.  
  7.  
  8. var difference = function(leftHour, rightHour){
  9.     if(!leftHour || !rightHour)
  10.         return 0;
  11.     spt = leftHour.split(":");
  12.     var totLeft = parseInt(spt[0],10) * 60 + parseInt(spt[1],10);
  13.     spt = rightHour.split(":");
  14.     var totRight = parseInt(spt[0],10) * 60 + parseInt(spt[1],10);
  15.  
  16.     if (totLeft < totRight) {
  17.  
  18.     alert("ko");
  19.     return 0;
  20.  
  21. }
  22.  
  23.     return totLeft - totRight;
  24. }
  25.  
  26. function Calcola()
  27. {
  28.  
  29.     var diff1 = difference(document.myForm.F.value, document.myForm.E.value);
  30.     var diff2 = difference(document.myForm.D.value, document.myForm.A.value);
  31.     var diff3 = difference(document.myForm.G.value, document.myForm.F.value);
  32.     var diff4 = difference(document.myForm.H.value, document.myForm.G.value);
  33.     var diff5 = difference(document.myForm.H.value, document.myForm.A.value);
  34.  
  35.     document.myForm.RISULTATO1.value = diff1
  36.     document.myForm.RISULTATO2.value = diff2
  37.     document.myForm.RISULTATO3.value = diff3
  38.     document.myForm.RISULTATO4.value = diff4
  39.     document.myForm.RISULTATO5.value = diff5
  40.  
  41.  
  42. }
  43. // -->
  44. </script>
  45.  
  46. </head>
  47. <body>
  48.  
  49. <form name="myForm">
  50.  
  51. A<input type="text" name="A" size="20" onkeyup="Calcola()">
  52. B<input type="text" name="B" size="20" onkeyup="Calcola()">
  53. C<input type="text" name="C" size="20" onkeyup="Calcola()">
  54. D<input type="text" name="D" size="20" onkeyup="Calcola()">
  55. E<input type="text" name="E" size="20" onkeyup="Calcola()">
  56. F<input type="text" name="F" size="20" onkeyup="Calcola()">
  57. G<input type="text" name="G" size="20" onkeyup="Calcola()">
  58. H<input type="text" name="H" size="20" onkeyup="Calcola()">
  59.  
  60. <br><br>
  61.  
  62. F-E<input type="text" name="RISULTATO1" size="20" value="0" readonly>
  63. D-A<input type="text" name="RISULTATO2" size="20" value="0" readonly>
  64. G-F<input type="text" name="RISULTATO3" size="20" value="0" readonly>
  65. H-G<input type="text" name="RISULTATO4" size="20" value="0" readonly>
  66. H-A<input type="text" name="RISULTATO5" size="20" value="0" readonly>
  67.  
  68.  
  69. </form>
  70.  
  71. </body>
  72.  
  73. </html>
  74.  
  75.  
Mar 14 '09 #4
viki1967
263 100+
Any suggestions ?
Viki
Mar 15 '09 #5
acoder
16,027 Expert Mod 8TB
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?
Mar 16 '09 #6
viki1967
263 100+
Try this... working... :)

Expand|Select|Wrap|Line Numbers
  1. <script> 
  2.  
  3. function isTime(str) { 
  4.     str="0"+str+":0"
  5.     spz = str.replace("\.",":").split(":"); // accetta anche il . come separatore
  6.     hh = parseInt(spz[0],10)
  7.     mm = parseInt(spz[1],10)
  8.     if ((hh == 24 && mm == 0) || (hh < 24 && mm < 60)) 
  9.         return (hh*60+mm);
  10.     return false;
  11.  
  12. function Calcola() {
  13.  for(var i=1; i<=7; i++) {
  14.     cx = ("ABCDEFGH").substr(i-1,1);
  15.     cy = ("ABCDEFGH").substr(i,1);
  16.     x = isTime(document.myForm.elements(cx).value);
  17.     y = isTime(document.myForm.elements(cy).value);
  18.     if(x && y) {
  19.         document.myForm.elements(cy).style.background='white';
  20.         if(y>x) {
  21.             document.myForm.elements("RISULTATO"+i).value = (y-x);
  22.         } else {
  23.             document.myForm.elements(cy).style.background = 'orange';
  24.         }
  25.     }
  26.  }
  27. }
  28. </script>
  29.  
  30. <form name="myForm">
  31.  
  32. A<input type="text" name="A" size="5" onkeyup="Calcola()">
  33. B<input type="text" name="B" size="5" onkeyup="Calcola()">
  34. C<input type="text" name="C" size="5" onkeyup="Calcola()">
  35. D<input type="text" name="D" size="5" onkeyup="Calcola()">
  36. E<input type="text" name="E" size="5" onkeyup="Calcola()">
  37. F<input type="text" name="F" size="5" onkeyup="Calcola()">
  38. G<input type="text" name="G" size="5" onkeyup="Calcola()">
  39. H<input type="text" name="H" size="5" onkeyup="Calcola()">
  40.  
  41. <br><br>
  42.  
  43. B-A<input type="text" name="RISULTATO1" size="5" value="0" readonly>
  44. C-B<input type="text" name="RISULTATO2" size="5" value="0" readonly>
  45. D-C<input type="text" name="RISULTATO3" size="5" value="0" readonly>
  46. E-D<input type="text" name="RISULTATO4" size="5" value="0" readonly>
  47. F-E<input type="text" name="RISULTATO5" size="5" value="0" readonly>
  48. G-F<input type="text" name="RISULTATO6" size="5" value="0" readonly>
  49. H-G<input type="text" name="RISULTATO7" size="5" value="0" readonly>
  50.  
  51. </form>
  52.  
  53.  
Mar 16 '09 #7
acoder
16,027 Expert Mod 8TB
Nice - thanks for posting!
Mar 16 '09 #8
Markus
6,050 Expert 4TB
Sorry for nit-picking; I'm bored.

Your scripts should be done like so:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3. [...]
  4.  
  5. </script>
  6.  
:D
Mar 16 '09 #9
viki1967
263 100+
@Markus
Many thanks !!! :-)
viki
Mar 18 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Marcus | last post: by
5 posts views Thread by Paul | last post: by
26 posts views Thread by Frank | last post: by
23 posts views Thread by Rudolf Bargholz | last post: by
6 posts views Thread by Sameer | last post: by
2 posts views Thread by John | last post: by
1 post views Thread by mimmo | last post: by
11 posts views Thread by stax | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.