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!

Modulus operator

Status
Not open for further replies.

trystanhuwwilliams

Programmer
Aug 23, 2002
39
0
0
GB
Hi,
I need to test for a leap year within a form - I could achieve this within 'C' using the modulus operator '%' -which tests for whole numbers & returns the remainder i.e
Code:
2000 % 4 = 0
2001 % 4 = 3
Is there an equivalent operator or function within Visual Basic?
I'd be grateful for any info,
Cheers,
Trystan
 
result = number1 Mod number2

Straight from the help files.
 
but is does NOT accuratly test for leap year. The 'rules' for leap year are somewhat more complex than the simplistic divisible by four!

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
That is true. If you want a more accurate test for leap year it would be:

result = myYear Mod 400

if result = 0 then it is a leap year

else

result = myYear Mod 100

if result = 0 then it is not a leap year

else

result = myYear Mod 4

if result = 0 then it is a leap year


Here is a leap year test that should be good for the next couple thousand years. After that I really don't care. [smile]

 
but ... but ... but ... why three tests? only one (correctly defined) one is necessary (hint - mod is NOT useful)

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
That may well be. I was answering the question. Really not up to me to say if someones method is better or worse than mine.

The above method will work fine. I would imagine that there are many ways to do this. One way might be to find out how many days between jan 1 and dec 31. If 366 then a leap year and then you let access handle the rules.

If you have a better method and would like to share then great. I will most likely save your method and the next time I need to know if it is a leap year I will use your method. I keep all usefull threads as text files. Never know when you might just need something.

Also, I never assume that mine way is best. In fact I always assume that there is a better way. The only thing I really worry about is that my way works without causing undue headache. I have seen many of your solutions that I have liked far better than mine. I usually start using those one from then on. I do not go back to re-write the exsitsing ones.
 
yes ... the check for the number of days, but be careful of the number of days you check for, as (depending on the details of your procedure) may not be 366 for a leap year (e.g. try it with datediff).


MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top