Mail Archives: djgpp/1997/03/21/04:56:41
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.
<Disclaimer: While this may not be the best example, it does
illustrate my point. I will happily accept any constructive
criticism.>
Jamie Ingram
jamiei AT potato DOT wa DOT gov DOT au
#include <stdio.h>
#include <string.h>
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;}
- Raw text -