Caculate age

Ben Patel benvpatel at yahoo.com
Tue Feb 4 03:52:34 EST 2003


Here is what I have done so far :
# This function calculates the age of a person based on a birthdate
# For example: cal_age('02/01/1970')
# Enter any birthdate in the format MM/DD/YYYY or MM-DD-YYYY
# Uses mx.DateTime interface from www.lemburg.com
import time
import re
from mx.DateTime import *
def cal_age(text):
 
 m = re.match("(\d{1,2})/(\d{1,2})/(\d{1,4})",text)
 n = re.match("(\d{1,2})-(\d{1,2})-(\d{1,4})",text)
 if m:
  mm = int(m.group(1))
  dd = int(m.group(2))
  yy = int(m.group(3))
  diff = Age(now(),Date(yy,mm,dd))
  print 'Your age: Years ',diff.years,'Months',diff.months,' Days',diff.days
 if n:
  mm = int(n.group(1))
  dd = int(n.group(2))
  yy = int(n.group(3))
  diff = Age(now(),Date(yy,mm,dd))
  print 'Your age: Years ',diff.years,'Months',diff.months,' Days',diff.days
 
Any comments or advise !
How can I make it accept any format (YYYY-MM-DD etc..)
By the way - This is not an assignment from a school or university class.
-Thanks !
     
 
 Laura Creighton <lac at strakt.com> wrote:Would knowing when _now_ is help?

>From the Python Library Reference 
http://www.python.org/doc/current/lib/lib.html

there is a module called time.
http://www.python.org/doc/current/lib/module-time.html

localtime and strftime look pretty interesting to me.

Laura


---------------------------------
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20030204/d54a8d9a/attachment.html>


More information about the Python-list mailing list