www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/03/21/04:56:41

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.

<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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019