[ create a new paste ] login | about

Link: http://codepad.org/uVufrI8d    [ raw code | output | fork ]

Python, pasted on Jan 22:
import datetime
def calculate_age(dob):
    today = datetime.date.today()
    if today > dob:
        return today.year - dob.year - 1
    else:
        return today.year - dob.year    
        
print calculate_age( datetime.date(1982, 9, 29)) # outputs 31
print calculate_age( datetime.date(1992, 2, 29)) # outputs 21
print calculate_age( datetime.date(2014, 1, 1))  # outputs -1 

def calculate_age(dob):
    today = datetime.date.today()
    if today.month < dob.month or (today.month == dob.month and today.day < dob.day):
        return today.year - dob.year - 1
    else:
        return today.year - dob.year    


print calculate_age( datetime.date(1982, 9, 29)) # outputs 31
print calculate_age( datetime.date(1992, 2, 29)) # outputs 21
print calculate_age( datetime.date(2014, 1, 1))  # outputs 0


Output:
1
2
3
4
5
6
31
21
-1
31
21
0


Create a new paste based on this one


Comments: