verifying date values

richard_chamberlain richard_chamberlain at ntlworld.com
Sat Jun 24 10:26:47 EDT 2000


Hi,

Marc Lemburg's mxDateTime module (do a search at www.vex.net/parnassus)
returns a RangeError if you create a date that doesn't exist. I've created a
checkDate method below which returns 0 or 1.

mxDateTime is an excellent module if you're going to be doing anything
complicated with dates or times.

Richard

import DateTime

def checkDate(y,m,d):
    "year,month,day"
    try:
        checkDate=DateTime.DateTime(y,m,d)
    except DateTime.RangeError:
        return 0
    else:
        return 1
def testDates(y,m,d):
    print '%d/%d/%d' %(y,m,d)
    if checkDate(y,m,d):
        print 'is ok'
    else:
        print 'is not valid'

def main():
    testDates(2000,3,10)
    testDates(2000,13,3)

if __name__=='__main__':
    main()


dsavitsk <dsavitsk at e-coli.net> wrote in message
news:PcQ45.16280$dF.645882 at news1.rdc1.il.home.com...
> is there a quick way to see if a string is a valid date? something like
>
> >>> isDate('5/22/1999')
> 1
>
> thanks
>
>
>





More information about the Python-list mailing list