-
Apr 18th, 2012, 05:36 AM
#1
Thread Starter
Fanatic Member
Time Difference
Hi guys.
I can't seem to find a proper code for getting the time difference.
This is the flow of the program:
1.) Input Time-in. (Should be in "0000" format. Example: "0800" [stands for 8:00 AM] )
2.) Input Time-out. (Example: "1700" [stands for 5:00 PM])
3.) Output the number of hours. (8 hours would be in this case)
The problem is with the minutes. In the flow of the code, i would normally go with "End - Start" and then divide with 100 to get the total number of hours. But if the first input is "759" (which stands for "7:59 AM") and the second input is "1702" (which stands for "5:02" PM), I always gets a result of "943" which is wrong and I'm kinda confused right now on how to get "902". Please help.
Thanks.
Manny Pacquiao once posted in his twitter:
It doesn't matter if the grammar is wrong, what matter is that you get the message
-
Apr 18th, 2012, 07:04 AM
#2
Re: Time Difference
I have made a pretty quick example in VB6:
vb.net Code:
Option Explicit
Private Sub Command1_Click()
'~~~ Sample values
Dim strStart As String
Dim strEnd As String
strStart = "759"
strEnd = "1702"
'~~~ Split the hours and minutes of both times
Dim lngMin1 As Long
Dim lngMin2 As Long
Dim lngHr1 As Long
Dim lngHr2 As Long
'~~~ Minutes
lngMin1 = CLng(Right$(strStart, 2))
lngMin2 = CLng(Right$(strEnd, 2))
'~~~ Hours
lngHr1 = CLng(Mid$(strStart, 1, Len(strStart) - 2)) '~~~ will take both "759" as well as "0759"
lngHr2 = CLng(Mid$(strEnd, 1, Len(strEnd) - 2))
'~~~ Find the total minutes of each time
Dim lngTime1 As Long
Dim lngTime2 As Long
lngTime1 = (lngHr1 * 60) + lngMin1
lngTime2 = (lngHr2 * 60) + lngMin2
'~~~ Subtract the total minutes
Dim lngDiff As Long
lngDiff = lngTime2 - lngTime1
'~~~ Now we got the difference. So, divide the minutes by 60 (and take floor() of it). This will give the minutes.
'~~~ ... and by taking MOD 60, you'll get the minutes
Dim strTimeDiff As String
strTimeDiff = CStr(Fix(lngDiff / 60)) & Format(CStr(lngDiff Mod 60), "0#") '~~~ "903"
'~~~ Display the time difference
MsgBox strTimeDiff
End Sub
I don't have C++ at the moment and it's a bit rusty. And, I hope you would be able to understand the VB6 code.
It's very simple code. And I hope you would be able to easily translate it to C++ code, once you get the idea.
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Apr 18th, 2012, 07:51 AM
#3
Thread Starter
Fanatic Member
Re: Time Difference
Thanks for the quick reply. I'll see what I can do.
I too am not familiar with C++.
Thanks again.
Manny Pacquiao once posted in his twitter:
It doesn't matter if the grammar is wrong, what matter is that you get the message
-
May 13th, 2012, 02:18 AM
#4
New Member
Re: Time Difference
Code:
#include<iostream>
using namespace std;
main()
{
struct tm //defines a structure for holding the hour and minute of he input and output
{
int hh;
int mm;
};
int inp,outp,res,a,b,c,d;
tm tm1,tm2,tm3;
cout<<"Enter the start time in the HHMM format"<<endl;
cin>>inp; //say 759
cout<<"Enter the end time in the HHMM format"<<endl;
cin>>outp; //say 1702
tm1.mm=inp%100; //59
tm1.hh=inp/100; //7
tm2.mm=outp%100; //2
tm2.hh=outp/100; //17
tm3.hh=tm2.hh-tm1.hh; //10
if((tm2.mm-tm1.mm)<0) //As in your case 2-59=-57
{
tm3.mm=60+(tm2.mm-tm1.mm); //so this becomes 3
tm3.hh--; //decrease hour by one
}
else
tm3.mm=tm2.mm-tm1.mm;
cout<<tm3.mm+tm3.hh*100<<endl; //outputs 903 to screen
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|