How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
As I did not see the correct implementation, I recoded mine this way...
Assumption of being "18" on the 28th of Feb when born on the 29th is just wrong. Swapping the bounds can be left out ... it is just a personal convenience for my code :)
Extend to Danny W. Adair Answer, to get month also
Unfortunately, you cannot just use timedelata as the largest unit it uses is day and leap years will render you calculations invalid. Therefore, let's find number of years then adjust by one if the last year isn't full:
Upd:
This solution really causes an exception when Feb, 29 comes into play. Here's correct check:
Upd2:
Calling multiple calls to
now()
a performance hit is ridiculous, it does not matter in all but extremely special cases. The real reason to use a variable is the risk of data incosistency.