loop date range

This is a discussion on loop date range within the C++ Programming forums, part of the General Programming Boards category; hello, I need to loop the days within a date range. Not sure where to begin. startDate: 01/01/1960 endDate: 02/06/2011 ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    4

    loop date range

    hello, I need to loop the days within a date range. Not sure where to begin.

    startDate: 01/01/1960
    endDate: 02/06/2011

    how do I convert the date into a number so I can loop?

    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,369
    I would use ctime; but, I am a C programmer; there might be a newer C++ library.
    ctime (time.h) - C++ Reference

    Tim S.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4
    hey, I've found this piece of code, and have been playing around with it. But it seems like I cant have a date less than 1970.... Now I'm stuck again

    Code:
       struct tm one = {0}, two = {0};
    
       one.tm_year = 71;    /* years since 1900 */
       one.tm_mon  = 0 - 1; /* months are 0 - 11 */
       one.tm_mday = 1;
    
       two.tm_year = 92;    /* years since 1900 */ 
       two.tm_mon  = 4 - 1; /* months are 0 - 11 */
       two.tm_mday = 3;
       
       time_t startTime = mktime(&one), endTime = mktime(&two);
       
       cout << (startTime / (60 * 60 * 24));

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    7
    I would recommend creating a Date class with a member function NextDay(). The function could increment the date in the correct manner.

    Example:
    Code:
    class Date
    {
    public:
        Date(int month, int day, int year);
        void NextDay();
        int day;        // you might want to use accessors/mutators instead. But this class is rather simple.
        int month;
        int year;
    };
    ...
    int main()
    {
        ...
        Date start(1, 1, 1960);
        Date end(2, 7, 2011);  // One past the end.
        for(; start!=end; start.NextDay())
            // Print your date here
    }

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    531
    Or you could come up with your own numeric representation for dates. For example you could says dates are represented by number of days elapsed since 1900. Then write a conversion function to take DD/MM/YY to that representation (and back, for printing).

    Using a Date class as suggested will probably yield nicer looking code where you use dates (and it is certainly the more C++ approach), but might be more work upfront depending on how much you want to do with dates. For example, in the "Date" snippet posted above, you'd need to implement operator!= .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two date objects showing same date?
    By dxfoo in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2010, 06:06 PM
  2. Show log entries in a date range
    By JizJizJiz in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2005, 05:33 PM
  3. range?
    By tu_user in forum C++ Programming
    Replies: 13
    Last Post: 01-19-2004, 10:35 PM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 04:23 PM
  5. Range
    By volk in forum C Programming
    Replies: 3
    Last Post: 12-19-2002, 08:43 AM

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