dealing with dates

This is a discussion on dealing with dates within the C++ Programming forums, part of the General Programming Boards category; I want to subtract date A from date B. Both are stored as mm/dd/yyyy. Googling on datediff - as known ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    dealing with dates

    I want to subtract date A from date B. Both are stored as mm/dd/yyyy.

    Googling on datediff - as known in vb and subtract dates all with c++ hasn't yields much.

    Any std or time.h functions or am I left to write my own?

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    The Boost Date-time library has functionality for this. You can literally subtract two date objects, using the minus operator.

    http://www.boost.org/doc/html/date_time.html

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You could put your yyyy, mm, and dd into a struct tm, use mktime to convert to a time_t, and then subtract two time_ts with difftime (if both dates are within the time's epoch). Something similar to this, perhaps.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    #include <iostream>
    #include <time.h>
    
    using namespace std;
    int main()
    {
        time_t current_time,past_time;
        
        
        time(&current_time);
        
        cout<<"Current time in time_t format is "<<current_time<<endl;
        cout<<"Current time in ctime format is "<<ctime(&current_time)<<endl;
        
        past_time = current_time -1000;
        cout<<"\nPast time in time_t format is "<<past_time<<endl;
        cout<<"Past time in ctime format is "<<ctime(&past_time)<<endl;
        
        
        
        cin.get();
        return 0;
    }
    You can also use the code available in the FAQ for actually timing how long your program takes to run.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    Thanks to all.

    Dave - that code is perfect.

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    There is always the option of using Julian dates, as well:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    #define GREGORIAN
    
    const char *DAY_OF_WEEK[] = { "Monday", "Tuesday", "Wednesday",
      "Thursday", "Friday", "Saturday", "Sunday" };
    
      
    void print_julian(double julian_date) {
      int Z = julian_date + 0.5;
      int W = (Z - 1867216.25) / 36524.25;
      int X = W / 4;
      int A = Z + 1 + W - X;
      int B = A + 1524;
      int C = (B - 122.1) / 365.25;
      int D = 365.25 * C;
      int E = (B - D) / 30.6001;
      int F = 30.6001 * E;
    
      int day_of_month = B - D - F;
      int month = E > 12 ? (E - 13) : (E - 1);//E - 1 or E-13 (must get number less than or equal to 12)
      int year = month <= 2 ? (C - 4715) : (C - 4716);//(if Month is January or February) or C-4716 (otherwise)
    
      cout << month << "/" << day_of_month << "/" << year << endl;
    }
    
    int main() {
      int month = 8,
          year = 2005,
          day = 23;
      double hour = 9,
          minute = 16,
          second = 30;
    
      int a = (14 - month) / 12;
      int y = year + 4800 - a;
      int m = month + (12 * a) - 3;
      
    #ifdef GREGORIAN
      // for a date in the Gregorian calendar (at noon)
      int jdn = day + 
        (((153 * m) + 2) / 5) +
        (365 * y) + 
        (y / 4) -
        (y / 100) +
        (y / 400) -
        32045;
    #else
      // for a date in the Julian calendar (at noon)
      int jdn = day + (((153 * m) + 2) / 5) + (365 * y) +
        (y / 4) - 32083;
    #endif
    
      double julian_date = jdn + ((hour - 12) / 24) + (minute / 1440) + (second / 86400);
      
      cout << "The day of the week is " <<
        DAY_OF_WEEK[static_cast<int>(julian_date) % 7] << "." << endl;
      
      cout << "Today is ";
      print_julian(julian_date);
      
      julian_date -= 40;
      cout << "Forty days ago it was ";
      print_julian(julian_date);
      
      return 0;
    }
    Quote Originally Posted by the output of that program
    The day of the week is Monday.
    Today is 8/23/2005
    Forty days ago it was 7/14/2005

  7. #7
    Dragoon Lover wyvern's Avatar
    Join Date
    Jul 2005
    Location
    dragooncity
    Posts
    28
    this code return this error :

    12 E:\WKC\Untitled1.cpp [Warning] converting to `int' from `double' .

    (if it is my mistake please teach me, im new to c++)

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by wyvern
    this code return this error :

    12 E:\WKC\Untitled1.cpp [Warning] converting to `int' from `double' .
    It is a warning (not an error), and I think its message is pretty clear. difftime returns a double, which is being implicitly converted to an int -- thus drawing the warning.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting days between 2 dates
    By nynicue in forum C Programming
    Replies: 12
    Last Post: 02-19-2010, 09:50 AM
  2. Generating Dates! Compiles but does not work...
    By ottomated in forum C Programming
    Replies: 11
    Last Post: 04-22-2008, 03:58 AM
  3. Getting the difference between dates
    By Leftos in forum C Programming
    Replies: 7
    Last Post: 01-19-2008, 11:49 PM
  4. Using dates in Pro*C
    By clancyPC in forum C Programming
    Replies: 0
    Last Post: 08-17-2006, 06:37 AM
  5. number of days between 2 dates.
    By explosive in forum C++ Programming
    Replies: 10
    Last Post: 02-17-2005, 06:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21