From: jamiei AT potato DOT wa DOT gov DOT au (Jamie Ingram) Newsgroups: comp.os.msdos.djgpp,comp.lang.c,alt.sb.programmer,alt.msdos.programmer,comp.os.msdos.programmer Subject: Re: Help with comparing dates Date: Fri, 21 Mar 1997 07:58:44 GMT Organization: Western Potatoes Lines: 63 Message-ID: <5gtf0l$lk3$1@opera.iinet.net.au> References: <5gq9ar$efi AT ruby DOT ucc DOT nau DOT edu> Reply-To: jamiei AT potato DOT wa DOT gov DOT au NNTP-Posting-Host: 203.59.16.1 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp mdr6 AT dana DOT ucc DOT nau DOT edu (Michael Dylan Ryan) spoke unto the masses, thusly: :-) 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. :-) -- :-) Michael D. Ryan [Dark Brotherhood Software] :-) Email: dbrotherhood AT geocities DOT com :-) mdr6 AT dana DOT ucc DOT nau DOT edu :-) TheShogun AT aol DOT com :-) Programming Page: http://www.geocities.com/SiliconValley/Pines/5301/ :-) -- G'Day Michael, Why not reorganise the date so that it is YY/MM/DD? Then do a string compare. The string compare will return an error level which you can use to determine if the date falls between the other two. See below. Jamie Ingram jamiei AT potato DOT wa DOT gov DOT au #include #include void swap(char &a, char &b) {char temp; temp = b; b = a; a = temp; return ;} int main(void) { char date[9],startDate[9], finishDate[9]; char temp; puts("\nEnter a date dd/mm/yy\n"); gets(date); swap(date[0], date[6]); swap(date[1], date[7]); puts("\nEnter start date dd/mm/yy\n"); gets(startDate); swap(startDate[0], startDate[6]); swap(startDate[1], startDate[7]); puts("Enter finish date dd/mm/yy\n"); gets(finishDate); swap(finishDate[0], finishDate[6]); swap(finishDate[1], finishDate[7]); if ((strcmp(startDate,date)<= 0)&& (strcmp(date,finishDate)<= 0)) printf("\nThe date is within the range of the other two dates.\n"); return 0;}