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

x
Solved

# Javascript Time Calculation

Posted on 2009-12-22
Medium Priority
473 Views
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;
}

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

</script>
``````
0
Question by:Cybervanes

LVL 14

Assisted Solution

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

Thanks
Ajitha
0

LVL 82

Assisted Solution

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

to:
var totalMin =  minOut-minIn ;
0

LVL 22

Expert Comment

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();
}
0

LVL 22

Expert Comment

ID: 26105590
Sorry.  Function above calculates seconds.  For hours, just divide by 3600.
0

LVL 22

Accepted Solution

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;
}
0

LVL 2

Author Closing Comment

ID: 31669022
Perfect!
0

LVL 2

Author Comment

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');
``````
0

LVL 2

Author Comment

ID: 26105793
0

LVL 22

Expert Comment

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).

0

LVL 22

Expert Comment

ID: 26105875
>> RE: Any good with PHP?

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

## Featured Post

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
Course of the Month19 days, 5 hours left to enroll