Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write a bulletproof date validation routine.
#11
These are formulas I came up with. I'll try to explain them.

ok when you test an equation, QB returns 0 (false) or -1 (true).

even numbered months before august (month#8) and odd numbered months after July (month#7) have 30 days (except feb, but that's covered in line 2)

So, line 1 is an equation that starts with 31 days and adds the validity of the above statement. If it's true, it subtracts 1, leaving 30 days.

Code:
DInM% = 31 + (m% MOD 2) * (m% > 7) + (m% MOD 2 XOR 1) * (m% < 8)

I'll break it apart:

Code:
31                      default value
m% MOD 2                returns 0 if date is even, 1 if date is odd
m% > 7                  returns -1 if date is > 7, 0 if date < 8
m% MOD 2 XOR 1          returns 0 if date is odd, 1 if date is even
m% < 8                  returns -1 if date is < 8, 0 if date > 7

now, we multiply the first two and the second two together:

Code:
(m% MOD 2) * (m% > 7)                returns -1 if date is odd and > 7
(m% MOD 2 XOR 1) * (m% < 8)          returns -1 if date is even and < 8

so if either one of these is true, 31 gets added to -1 and 0 for a result of 30
otherwise, 31 gets added to 0 and 0 for a result of 31
since both products can't be -1, you never end up with 31+ -1 + -1 = 29.

Code:
IF m% = 2 THEN DInM% = 28 - ((y% MOD 4 = 0) + (y% MOD 100 = 0) * NOT (y% MOD 400 = 0))

This line does pretty much the same thing, only it starts with 28 as the default value and adds 1 if:

1. the year is evenly divisible by 4, unless the year is evenly divisible by 100 and not evenly divisible by 400.

Code:
28                      default value
y% MOD 4 = 0            returns -1 if year is divisible by 4, otherwise 0
y% MOD 100 = 0          returns -1 if year is divisible by 100, otherwise 0
y% MOD 400 = 0          returns -1 if year is divisible by 400, otherwise 0

I hope this clears it up!

*peace*

Meg.

edit: another alternative to line #1 is:

Code:
DinM% = VAL(MID$("312831303130313130313031", m% * 2 - 1, 2))

so using your bit of code (which i like!) it would be this:

Code:
DinM% = VAL(MID$("312831303130313130313031", m% * 2 - 1, 2))
IF DinM% = 2 then DinM% = 28 - ((m% MOD 4 = 0 AND m% MOD 100 <> 0) OR (m% MOD 400 = 0))
Reply


Messages In This Thread
My entry - by Meg - 10-11-2004, 09:03 PM
updated - by Meg - 10-12-2004, 08:01 PM
Write a bulletproof date validation routine. - by Meg - 10-17-2004, 08:49 PM
/nod - by Meg - 10-23-2004, 01:34 AM
Foo - by ToohTooh - 10-26-2004, 10:36 PM
Reply on Clarification Suggestion - by Neo - 10-26-2004, 11:37 PM
Re: Reply on Clarification Suggestion - by oracle - 10-27-2004, 02:00 AM
Hmm... - by ToohTooh - 10-27-2004, 02:12 PM
Re: Hmm... - by Neo - 10-27-2004, 10:12 PM
Thank you. - by ToohTooh - 11-01-2004, 04:54 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)