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
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 ...
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
I would use ctime; but, I am a C programmer; there might be a newer C++ library.
ctime (time.h) - C++ Reference
Tim S.
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));
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 }
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!= .