From: Guenter Nagler Newsgroups: comp.os.msdos.djgpp,comp.lang.c,alt.msdos.programmer,comp.os.msdos.programmer Subject: Re: Help with comparing dates Date: Fri, 21 Mar 1997 09:30:09 +0100 Organization: Joanneum Research Lines: 102 Message-ID: <33324711.B29@ihm.tu-graz.ac.at> References: <5gq9ar$efi AT ruby DOT ucc DOT nau DOT edu> Reply-To: gnagler AT ihm DOT tu-graz DOT ac DOT at NNTP-Posting-Host: fiicm1pc21.tu-graz.ac.at Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------401570504756" To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp This is a multi-part message in MIME format. --------------401570504756 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Michael Dylan Ryan wrote: > Can someone help me with a function to compare two dates. I want to write > a function that will take a date string char *d = "03/01/97" and two other > date strings, start and end, and check to see if d is in between them. > > I have been working with dates for a while and can never get a function to > compare accurately 100% of the time. Any help appreciated. First convert the strings to date structures (day=1..31, month=1..12, year=1900..2099). You can write date compare routines for two dates date_less(date1, date2), date_equal(date1, date2), date_lessequal(date1,date2) and then use these to implement date_between(d, start, end) = date_lessequal(start, d) and date_lessequal(d, end) Another solution is converting dates to a number that counts days since a certain year and then compare the days, see attachment for this variant. This variant is for extended use of dates, e.g. find week day, find number of days between two dates ... --------------401570504756 Content-Type: text/plain; charset=us-ascii; name="DATE.C" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="DATE.C" /* conversion day/month/year to number of days since 1.1.1990 */ int daysperyear(int year) { return 365 + ((year % 4 == 0 || year % 100 == 0 || year % 400 == 0) ? 1 : 0); } int monthdays(int month, int year) { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: return daysperyear(year) == 365 ? 28 : 29; default: return 0; } } /* day=1..31 month=1..12, year=1900..2099 */ long dayssince1900(int day, int month, int year) { unsigned long d = 0; for (int y = 1900; y < year; y++) d += daysperyear(y); for (int m = 1; m < month; m++) d += monthdays(m, year); d += day-1; return d; } #include int main() { /* example: test if a day is between two days */ int start_day = 13, start_month = 5, start_year = 1967; int end_day = 21, end_month = 3, end_year = 1997; int test_day = 29, test_month = 2, test_year = 1997; long start_since1900 = dayssince1900(start_day, start_month, start_year); long end_since1900 = dayssince1900(end_day, end_month, end_year); long test_since1900 = dayssince1900(test_day, test_month, test_year); if (test_since1900 < start_since1900) printf("before"); else if (test_since1900 > end_since1900) printf("after"); else printf("between"); return 0; } --------------401570504756--