Getting diffirence of two time value.

Good day Guys.!

I just want to know on how to get the difference between two diffirent time value.

ex. time1=8:23:35 AM
time2=5:35:26 PM

Thank you for giving time and God bless.
After a dustup in the lounge some time back, there was an informal agreement to not discuss religion on this forum. So
God bless
is inadvisable IMO.
C++ has no dms class but you might be able to do it with time_t. You might also be able to define yourself a class that contains an h, m and s, and do it yourself.
http://www.cplusplus.com/reference/clibrary/ctime/time_t/
C++ really needs to replace some of these C libraries.
Thank you.. The user will enter the login time. and he will also enter the logout time. The diffirence of two time will be stored in in diftime variable.

Do you mean i will build a class that will parse the time with a (:) parameter.? Please help me build the class. I dont have any idea about classes.

Thank you.
Ah. If you have no experience with even pods (plain old data types) then you'll be better off trying time_t. I'm not sure if it has any overloaded operators for + and -, but it might. Try that.
Alternatively, and this is if you're really bent on drawing things out....
Get two strings. Parse the string into three integers: an hour value, minute, and a second, by cycling through characters and splitting on the colons. Add up the two second values and take the modulus of them (remainder) over 60. Set your total seconds to that, and calculate how many times sixty goes into the second count, then add that to your total minutes count... so it might look a little like this. It's going to be quite messy and unpleasant but teaching yourself structures and classes will probably take longer, although it'll be more useful in the long run:
1
2
3
4
5
totalhrs = hrs1 + hrs2;
totalmin = min1 + min2;
totalhrs += (totalmin % 60);
totalmin -= ((totalmin / 60) * 60); // cast to integer and then go back, it is messy
etc
Thank you for this guide. Ill try this right away..Haha.. Got to learn classes beginning today.

By the way, can i just know the process of parsing the time and store it in an array variable.hehe..pls

Thank you & God bless.
Dunno. There's no explanation on the C libs and I am no expert or proponent on their usage. Sorry. They're generally designed for use with time(), the <ctime> function. You'll just have to look it up.
Ok.thanks.by the way how can i give credtis of my post that was solved.hehe
Unless everything occurs on the same day you need to keep track of the days as well. In addition it would be better to use the 24 hour format rather than 12 hour format with AM and PM.
If you log on day 1 at 23:24:25 and you log off in day 2 at 07:08:08 you need to add 24 hours to the log off time before subtracting the log on time.
So the period on duty is: 31:08:08 - 23:24:25 = 07:43:43
Your code needs to reflect this possibility as well error handling for hours exceeding 23, minutes exceeding 59 and seconds exceeding 59.
Last edited on
Topic archived. No new replies allowed.