[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Javascript Time Calculation

Posted on 2009-12-22
10
Medium Priority
?
473 Views
Last Modified: 2012-05-08
I'm building a JavaScript function that will calculate the hours between two times. time format is military time.

the below code should output `2.25`. instead it outputs `2.75`.

Any Ideas what I've done wrong, or what I'm missing.
<script type="text/javascript" language="javascript">
function calcHours(timein,timeout){
    var timeIn = timein.split(':');
    var timeOut = timeout.split(':');
    var timeInF = parseInt(timeIn[1])
    var timeOutF = parseInt(timeOut[1]);
    var minIn = timeInF / 60;
    var minOut = timeOutF / 60;
    var totalMin = minIn + minOut;
    var hourIn = parseInt(timeIn[0]);
    var hourOut = parseInt(timeOut[0]);
  if(hourIn < hourOut){
     var totalHours = hourOut - hourIn;
  }
  if(hourIn > hourOut){
     var totalHours = 24 - hourIn;
     totalHours = totalHours + hourOut;
  }
  totalHours = totalHours + totalMin;
  alert(totalHours);			  
}

calcHours('6:15','8:30');

</script>

Open in new window

0
Comment
Question by:Cybervanes
10 Comments
 
LVL 14

Assisted Solution

by:ajitha75
ajitha75 earned 600 total points
ID: 26105437
line 9 should be
var totalMin = minOut - minIn;

Thanks
Ajitha
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 600 total points
ID: 26105446
change:
var totalMin =  minIn+minOut ;

to:
var totalMin =  minOut-minIn ;
0
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 26105568
Why all the math?  Why not just use Date class like this:

function calcHours(timein,timeout) {
      var date1 = new Date('1/1/2009 ' + timein);
      var date2 = new Date('1/1/2009 ' + timeout);
      var sec = date2.getTime() - date1.getTime();
      alert(sec);
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 26105590
Sorry.  Function above calculates seconds.  For hours, just divide by 3600.
0
 
LVL 22

Accepted Solution

by:
Bill-Hanson earned 800 total points
ID: 26105609
Oops, wrong again.  It calcs milliseconds, so divide by 3600000.

function calcHours(timein,timeout) {
      var date1 = new Date('1/1/2009 ' + timein);
      var date2 = new Date('1/1/2009 ' + timeout);
      var sec = (date2.getTime() - date1.getTime()) \ 3600000;
      alert(sec);
}
0
 
LVL 2

Author Closing Comment

by:Cybervanes
ID: 31669022
Perfect!
0
 
LVL 2

Author Comment

by:Cybervanes
ID: 26105740
alright Bill...

lets say time in is '23:15' and and time out is 3:30.

how would i get that to output the correct number of hours?

 
calcHours('23:15','3:30');

Open in new window

0
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 26105859
Well, it depends on what you consider 'correct'.

Should the code assume that the second time is for the next day and return 4.25?  If so, how do we know that the second time is not 2 or 3 days away?  If the times can span multiple days, you really need to pass the date into the function.  If we can assume that the second date always follows the first date and is no more than 1 day away, we can handle that with a couple of lines of extra code.

Should the code just return the difference (-19.75)?  (probably not, since that is what it does now).

Should the code return the absolute value of the difference (19.75).  (this is easy enough).

The answer really depends on your implementation.
0
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 26105875
>> RE: Any good with PHP?

No, but if there's a Date object available, you should use it.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question