How to find out if february has 29 or 28 days ?

Eric Brunel eric.brunel at pragmadev.com
Tue Jul 30 12:48:55 EDT 2002


Marcus Vinicius Laranjeira wrote:
> Folks,
> 
> I use the mx.DateTime package, and I need to know if in one particular
> year has a february with 28 or 29 days ? I don't know how to do that !

A year is a leap year if it can be divided by 4, except if it can be 
divided by 100, where it's a leap year only if it can be divided by 400. It 
sounds awful to compute, but in Python, it's actually quite simple:

>>> isLeap = lambda x: x % 4 == 0 and (x % 100 != 0 or x % 400 == 0)
>>> isLeap(1980)
1
>>> isLeap(1991)
0
>>> isLeap(2000)
1
>>> isLeap(1900)
0

HTH
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com



More information about the Python-list mailing list