-
Easy(?) Problem.
I have been writing a program and I came across a problem and have scrapped my code so many times, here is an example of what im trying to do.
I have the date and time setup, say for example the 6th of October at 21:57.
I wrote the code to subtract that date from the current date to give me how long its been since, it worked fine till it ticked over midnite, then it said 1 day! hehe i can see why it said one day, as "7 - 6 = 1", but it really has only been 2 hours and 3 minutes. Any suggestions on how to do this to actually show the correct amount of time since, i wrote some more code but that seemed to get screwed up somewhere, I'm trying to make the code short and sweet, because the idea i have in my mind is a long idea, where i get the total amount of hours and work from there, as you can see.. ouch! hehe So any of you got a good solution or if you find this really easy which I bet most of you would post me some code examples?
Thankyou for you time.
-
Whu don't you post some (commented code)code, so I can see what's the problem.
I think you can make use of the % operator, but I'm not sure.
-
hmz, surely that would give the same effect as when the time goes over 3hrs (past midnight) that the day would = 1, or did u mean convert it all into seconds and then work out how many hours are within that, and from the hours work out how many days have past etc?
If possible could you supply an example.
-
Understandably, the logic can get murky here until you break it down into manageable parts.
Given that you've taken Salem's suggestion of finding the difference in times, you'll want to determine if that difference is greater than 24 hours (86,400 seconds) - the whole number of days that may have passed.
Assuming the difference is less than 24 hours, you'll need to determine if the starting time plus the difference exceeds 2400 hours (midnight). If not, you're in good shape, day-wise.
If it does run past 2400 hours, you'll have to increment the day while limiting your output to hours/minutes/seconds. (This is why I would recommend determining the whole number of days that may have passed first. That is, if exactly 86,400 seconds have elapsed, the "clock" will show the same as your original start time, though one day will have gone by.)
Offhand, I would suggest that you write two separate functions. One to handle time differences of greater than 24 hours and one for less than 24 hours. The handy part of this is that you can call the "less than" function from the "greater than" function to handle portions of a day without duplicating your code.
Now, without trying to turn this into a bigger problem for you than it is, have you determined how you'll handle potential changes in the month? Let's say it's the 31st of October rather than the 6th. Food for thought? :)
-Skipper
-
Why don't you do
(7-1) - 6
Then it will have to cycle thru a whole day before your output = 1
-
I'm not certain I understand where you're going, cid666.
For example:
1st Start date/time:
October 6 / 21:00:00 hours
Elapsed time:
23 hours
1st End date/time:
October 7 / 20:00:00 hours
2nd Start date/time:
October 6 / 21:00:00 hours
Elapsed time:
25 hours
2nd End date/time:
October 7 / 22:00:00 hours
I inferred from Scorpion-ice's original post that the number of days was being determined by subtracting the start day from the end day, which isn't appropriate.
Your solution gets by for my first example, but not the second. ;)
-Skipper