Results 1 to 4 of 4

Thread: Time Difference

  1. #1

    Thread Starter
    Fanatic Member louvelle's Avatar
    Join Date
    Jun 2008
    Posts
    513

    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

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Time Difference

    I have made a pretty quick example in VB6:
    vb.net Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     '~~~ Sample values
    5.     Dim strStart As String
    6.     Dim strEnd As String
    7.        
    8.     strStart = "759"
    9.     strEnd = "1702"
    10.    
    11.    
    12.     '~~~ Split the hours and minutes of both times
    13.     Dim lngMin1 As Long
    14.     Dim lngMin2 As Long
    15.    
    16.     Dim lngHr1 As Long
    17.     Dim lngHr2 As Long
    18.    
    19.     '~~~ Minutes
    20.     lngMin1 = CLng(Right$(strStart, 2))
    21.     lngMin2 = CLng(Right$(strEnd, 2))
    22.    
    23.     '~~~ Hours
    24.     lngHr1 = CLng(Mid$(strStart, 1, Len(strStart) - 2)) '~~~ will take both "759" as well as "0759"
    25.     lngHr2 = CLng(Mid$(strEnd, 1, Len(strEnd) - 2))
    26.    
    27.    
    28.     '~~~ Find the total minutes of each time
    29.     Dim lngTime1 As Long
    30.     Dim lngTime2 As Long
    31.    
    32.     lngTime1 = (lngHr1 * 60) + lngMin1
    33.     lngTime2 = (lngHr2 * 60) + lngMin2
    34.    
    35.     '~~~ Subtract the total minutes
    36.     Dim lngDiff As Long
    37.     lngDiff = lngTime2 - lngTime1
    38.    
    39.     '~~~ Now we got the difference. So, divide the minutes by 60 (and take floor() of it). This will give the minutes.
    40.     '~~~ ... and by taking MOD 60, you'll get the minutes
    41.     Dim strTimeDiff As String
    42.     strTimeDiff = CStr(Fix(lngDiff / 60)) & Format(CStr(lngDiff Mod 60), "0#") '~~~ "903"
    43.    
    44.     '~~~ Display the time difference
    45.     MsgBox strTimeDiff
    46. 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,...

  3. #3

    Thread Starter
    Fanatic Member louvelle's Avatar
    Join Date
    Jun 2008
    Posts
    513

    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

  4. #4
    New Member
    Join Date
    May 2012
    Posts
    4

    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
  •  



Click Here to Expand Forum to Full Width