Caculate age

Mike C. Fletcher mcfletch at
Sun Feb 2 01:00:02 CET 2003

    Break down the problem into smaller steps:
        given a birthday in "any format", convert it to a "date" value, 
think something simple, such as (year,month,day) as integers
        figure out what "age" means, and create an algorithm to 
calculate it for a birthday which is expressed as a date value
            you need to be able to get today's date in your prefered 
date format, and the time module can do things like that)
            you need to be able to handle cases where the birthday is 
still ahead in the year, and when it is already passed in the year (and 
decide what to do on the birthday itself, normally it's considered part 
of the following year)

    Watch out for the time module's inability to deal with dates "before 
the epoch" (1970)

    If you can define "any format" as "any format you like" (as opposed 
to it meaning "every format"), then you can skip the hardest part of the 
problem (parsing "any format" for dates is something that just doesn't 
have a good solution, given that huge number of formats with ambiguities 
between formats).

    I'm assuming you're not being asked to take UTC vs local-time into 
account, and similarly that you don't need hour or minute of birth (to 
figure out "age" on the person's birthday).  If you do, and you're 
asking a question this poorly defined the night before... well... sucks 
to be you ;) .

    If you're allowed to use third-party modules (not likely, but you 
didn't mention it), consider mx.DateTime, where this is a 
straightforward 4 to 6 line script, depending on your coding style (for 
most reasonable definitions of "any format").

Anyway, good luck, and watch out for leaving projects to the last moment 
:) ,

Ben wrote:

>I am new at Python. I have an assignment to be written by tomorrow in Python.
>- Calculate a person's age based on a brithdate in any format
>Any help will be appreciated.
>Thanks in advance

  Mike C. Fletcher
  Designer, VR Plumber, Coder

More information about the Python-list mailing list