Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Calculate time between two dates - c++

Options
  • 24-03-2006 1:53am
    #1
    Closed Accounts Posts: 1,061 ✭✭✭


    Is there any function to do this?
    I have two dates stored in int variables:
    int day;
    int month;
    int year;
    
    int day2;
    int month2;
    int year2;
    

    Is there any way(function?) to work out say.. how many days from one date to the next?
    Or will I have to make my own function?
    I have looked up difftime() and mktime(), but I'm not sure if they will do what I want them to(I don't really understand them).
    thanks.


Comments

  • Registered Users Posts: 1,996 ✭✭✭lynchie


    Haven't done this in a long time, but i tihnk what u need to do is the following.

    Create a tm struct for both dates
    struct tm
    {
      int tm_sec;                   /* Seconds.     [0-60] (1 leap second) */
      int tm_min;                   /* Minutes.     [0-59] */
      int tm_hour;                  /* Hours.       [0-23] */
      int tm_mday;                  /* Day.         [1-31] */
      int tm_mon;                   /* Month.       [0-11] */
      int tm_year;                  /* Year - 1900.  */
      int tm_wday;                  /* Day of week. [0-6] */
      int tm_yday;                  /* Days in year.[0-365] */
      int tm_isdst;                 /* DST.         [-1/0/1]*/
    

    then use mktime to return you a time_t struct for both times.

    Then use difftime to give you the number of seconds difference between the two dates..

    Then multiply by 60*24 to find out the number of days difference.


  • Closed Accounts Posts: 1,061 ✭✭✭dawballz


    Ok I got it all working.. here is my code:
        double futureDate, pastDate;     //have tried int and float
    
    
    
    /* dateEntered struct set up here *********/
    time_t entered = mktime( &dateEntered );
    time_t now = time(0);
    
    
    
    if(now > entered)    //if the date is in the past
    {
           pastDate = ((now - entered) / 60.0 / 60.0 / 24.0);
           cout.precision(2);
           cout << "That date was " << pastDate << " days ago" << endl;
           retrval = 1;           //return 0 
    }
    else                  //otherwise()
    {
        retrval = 0;           //return 1
        cout << "ENTERED - NOW: " << entered - now << endl;
        futureDate = ((entered - now) / 60.0 / 60.0 / 24.0);
        cout.precision(2);
        cout << "That date is in " << futureDate << " days" << endl;
    }
    

    Now, it all works grand, until you enter a date that is over (Approx ) 80 days from now..
    See:
    Enter day, month, year: 27 08 2006
    ENTERED - NOW: 13202427
    That date is in 1.5e+002 days

    AND(for comparison):
    Enter day, month, year: 23 04 2006
    ENTERED - NOW: 2315796
    That date is in 27 days

    Same happens if an date 80 days ago is entered.
    I suspect it has something to do with a variable(pastDate/futureDate) not being able to hold what it is supposed to be holding.

    A quick response would be lovely.
    Thanks.


  • Closed Accounts Posts: 1,061 ✭✭✭dawballz


    Anyone.....?


  • Registered Users Posts: 1,996 ✭✭✭lynchie


    Your code looks fine, but you need to format the output using cout. See here for more info, but the following should work.
    cout.setf(ios::fixed, ios::floatfield);
    cout.setf(ios::showpoint);
    


Advertisement