Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math Problem

Status
Not open for further replies.

TheConeHead

Programmer
Joined
Aug 14, 2002
Messages
2,106
Location
US
I am having a brain freeze - I need to figure out an amount of time given someone sstart time and ending time on a 24 hour clock - so if I have:

Start Time: 0000
Ending Time: 0730

I need a result of: 7.5 hours

or if I have:

Start Time: 0900
Ending Time: 1300

I need a result of: 4 hours

How should I go about this?


[conehead]
 
Well, if the second number is greater than the first (and represent times on the same day), then subtract the first from the second. The result (let's call it x) can be used to determine the number of hours and fraction of hours as:

Code:
var hours = x/100;
var fraction = (x%100)/60;
var diff = ""+hours + "." + fraction;

Untested, but this seems like it should work.

'hope that helps.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
So how can I change 10.3 to 10.5 - is there a mathematical solution or do I need to do it string-wise?

[conehead]
 
Divide 1 by the result of 0.6 / the minute portion, so:

To convert .3 (30 mins) to decimal:

Code:
1 / (.6 / .3) == .5

To convert .15 (15 mins) to decimal:

Code:
1 / (.6 / .15) == .25

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
What's "10.3" represent? Is it 10 hours and 30 minutes? Doing it my way should for that 30 minutes to come out as .5 (notice the division by 60).

But now I'm re-thinking my solution. Simple subtraction will not help you since the last two digits are base 60.

Code:
var earlier = 645;
var later = 1015;
var diff = later - earlier; //370... uh, oh!

New approach:

Code:
var earlier = 645;
var earlierHour = earlier/100; //6
var earlierMin = earlier%100; //45
var later = 1015;
var laterHour = later/100; //10
var laterMin = later%100; //15
var diffHour = laterHour - earlierHour; //4
//NOTE:  if laterHour might be the next day, early morning, you need to program for special cases.
if(laterMin < earlierMin)
 laterMin += 60; //75
var diffMin = laterMin - earlierMin; //30
var fractionHour = diffMin/60; //0.5
var diffTime = diffHour+diffMin; //4.5


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
I'd convert them to date variables and let it do the minute difference for me. Here's an example using a bit of Dave's code:
Code:
<script type="text/javascript">

var startTime = 0900;
var start = new Date();
start.setHours(Math.floor(startTime/100));
start.setMinutes(Math.floor(startTime%100));
var endTime = 1300;
var end = new Date();
end.setHours(Math.floor(endTime/100));
end.setMinutes(Math.floor(endTime%100));
//this gives you the difference in minutes
var diffMins = (end.getTime() - start.getTime()) / 60000
//convert the minutes to hours in decimal form
var diff = Math.floor(diffMins/60) + ((diffMins%60) / 60)
alert(diff);

</script>

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Good show, kaht! Easily expandable if the different may be across days also!

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Thanks [thumbsup2]

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Did this solve your issue conehead?

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
op wrote:
>So how can I change 10.3 to 10.5 - is there a mathematical solution or do I need to do it string-wise?
[tt]
var x="10.3"; //or x=10.3 etc
var y=(Math.round(x*2)/2).toFixed(1);
[/tt]
For any real number, the resultant number belongs to: {..., -0.5, 0.0, 0.5, 1.0, ...}.
 
Looking back over my code I realize I copy/pasted where I shouldn't have. No need for the Math.floor when doing modular division - it will always return an integer unless one of the numbers in the equation is a decimal. So you can get rid of this part:
Code:
<script type="text/javascript">

var startTime = 0900;
var start = new Date();
start.setHours(Math.floor(startTime/100));
start.setMinutes([!][s]Math.floor([/s][/!]startTime%100[!][s])[/s][/!]);
var endTime = 1300;
var end = new Date();
end.setHours(Math.floor(endTime/100));
end.setMinutes([!][s]Math.floor([/s][/!]endTime%100[!][s])[/s][/!]);
//this gives you the difference in minutes
var diffMins = (end.getTime() - start.getTime()) / 60000
//convert the minutes to hours in decimal form
var diff = Math.floor(diffMins/60) + ((diffMins%60) / 60)
alert(diff);

</script>

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
var start = 0000
var end = 0730
var diff = (end - start);
var hours = math.floor(diff/100);
var minutes = (diff % 100)*0.6
var time = hours+minutes

alert("you took "+time+" hours");

not tested but i think it works

 
var start = 0000
var end = 0730
var diff = (end - start);
var hours = math.floor(diff/100);
var minutes = (((diff % 100)*100)/60)
var time = hours+minutes

alert("you took "+time+" hours");

ok it works
 
var start = 0000
var end = 0730
var diff = (end - start);
var hours = math.floor(diff/100);
var minutes = (((diff % 100)*100)/60);
var time = hours+minutes;

alert("you took "+time+" hours");

a few semi colmans missing but i think this is pretty simple and returns answer if the start and ending times are inputed
 
mattasouths - what if start time is 0031 and end time is 0730? diff would then equal 699 - it's really best to leave it to using Date variables - that way you don't have to code for all those special cases.

Additionally, it's [!]M[/!]ath.floor, it's case sensitive and won't work if the m is lower-case.

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
i was just giving a basic answer i think forgeting to put a capital in Math.floor isn't much of a big deal and would be noticed when the person who uses it would see it.

Additionally i congratulate you on your accomplishment of successfully writing a great piece of code i was never trying to challenge you just trying to help.
 
i was never trying to challenge you just trying to help
And I wasn't trying to put your answer down - instead I was just showing how it could falter. For instance, if the OP decided to use your answer because it had way less lines than my solution it's probably best that he's aware of how it could break. This website is not a big competition and nobody is trying to insult you for pointing out bugs in your code. Rather, having those mistakes pointed out makes us all better coders. If you run into this same situation in the future you now know to code around that problem - and you already know the solution.

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top