473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2653
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

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

Similar topics

2
by: Marcus | last post by:
I am having some problems with trying to perform calculations on time fields. Say I have a start time and an end time, 1:00:00 and 2:30:00 (on a 24 hour scale, not 12). I want to find the...
0
by: GerritM | last post by:
I downloaded and tried the recently announced dirsync program. It looks nice, but... I have two presumably identical directories: one at home and one at work. I keep them synchronized by means...
5
by: Paul | last post by:
Hi All, I know this is probably simple, but I'll ask anyway. I have a form which when submitted to the database the field in the database automatically adds date and time which is fine. My only...
26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
23
by: Rudolf Bargholz | last post by:
Hi, I have a ralatively simple SQL: select FK from TABLE where upper(A) like 'B%' and upper(C) like 'D%' We have DB2 UDB v7.1 FP 12 installed on Linux and on Windows 2003 On Linux using...
6
by: Sameer | last post by:
Hi, I have a table which is something like this : Date Hour Minute Name Cost 12/12/01 2 4 ABC 15 12/12/01 2 15 ABC 7 12/12/01 2 55 ABC 8...
2
by: John | last post by:
Hi I am trying to find difference between two time values in hours. I have tried the following? ? dateDiff("h", #23:00:00#, #23:00:00#) gives 0 as expected. ? dateDiff("h", #00:00:00#,...
1
by: mimmo | last post by:
I have a problem while calculating timezone (from Europe/Rome to Brazil/East). Can someone help me ?? Thank you, Mimmo. On this site http://www.timezoneconverter.com/cgi-bin/tzc.tzc I have...
11
by: stax | last post by:
Hi, Assuming having a method that calls another method which also calls other methods and so on having a long winded tree of methods. What do I do in case it turns out everything has to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.