SORU
7 ŞUBAT 2010, Pazar


Python, doğum tarihi yaş

Nasıl bugünün tarihinden itibaren python bir yaş ve bir kişinin doğum tarihi nasıl bulabilirim? Bu doğum tarihi Django modeli DateField bir.

CEVAP
14 ŞUBAT 2010, Pazar


from datetime import date

def calculate_age(born):
    today = date.today()
    try: 
        birthday = born.replace(year=today.year)
    except ValueError: # raised when birth date is February 29 and the current year is not a leap year
        birthday = born.replace(year=today.year, month=born.month 1, day=1)
    if birthday > today:
        return today.year - born.year - 1
    else:
        return today.year - born.year

Güncelleme:Daha iyi Danny's solution kullanın

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Doug Bernards

    Doug Bernard

    7 Kasım 2007
  • khloe brooks

    khloe brooks

    25 Temmuz 2011
  • kindlechatmail

    kindlechatma

    25 AĞUSTOS 2010