Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date entry evaluation

Status
Not open for further replies.

DanJC

Programmer
Dec 6, 2001
19
0
0
SE
Folks,

There are probably better ways than this but I hope this will spark some ideas.

If you want to test a users date input for validity, try using this to define your years!!

Struct Year
{
struct month
{
int no_of_days;
char *name; //optional field if you want month names
} months[12];
} leapyear, nonleapyear; //define year data in memory;

void main()
{
int i;
for(i = 0;i<12;i++)
{
switch(i+1)
{
case 1: nonleapyear.months.no_of_days = 31;
leapyear.months.no_of_days = 31;
break;
case 2: nonleapyear.months.no_of_days = 28;
leapyear.months.no_of_days = 29;
break;
.....
}
}

if you now get a user entry then you can compare string sections to the year variables month data, e.g. if month is greater than 12 then reject entry or (to put something in code) if day value is invalid, reject entry.

if((<compare value> > nonleapyear.months[month_id].no_of_days) ¦¦ (<compare value> < 1)
{
<error routine>
}
else
{
<accepted routine>
}

If anyone has any other ideas on this then please put them on this thread because dates are pretty complicated and I don't think I know enough to get the best out of them.

&quot;Remember.. it's all gibberish until someone puts it in terms YOU understand...&quot;

DanJC
 
leapyear can be defined by

year%4 == 0

Just a side note :)

Matt
 
Actually leap year is a bit more complicated than that:
Code:
   if (year mod 4 != 0)
       {!leap yaer}
   else if  (year mod 400 == 0)
       {leap year}
   else if (year mod 100 == 0)
       {!leap year}
   else
       {leap year}
Also, approx every three years a leap second is declared to make things just a bit more accurate... but I don't think this part of the calculation is really necessary.
MY[red]enigma[/red]SELF:-9
myenigmaself@yahoo.com
 
Worth noting that 100 and 400 are both factors of 4 so you do only need to use x % 4.

You can expand the struct to include different month name formats.
 
Worth noting that 100 and 400 are both factors of 4 so you do only need to use x % 4.

You can expand the struct to include different month name formats and if you're brave use binary tree search
 
If you look at my algorithm closer you will note that if a year is divisible by 400 it is a leap year, but then if it is divisible by 100 it is not. I repeat, if it is divisible by 100 and not 400 it is not a leap year. You didn't look at the algorithm close enough. The year 1000 was not a leap year. It is divisible by 100 but not 400. The year 2000 is a leap year. It is divisible by 100 and 400. As you can see, I hope, you cannot just use %4. MY[red]enigma[/red]SELF:-9
myenigmaself@yahoo.com
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top