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

  • Distractify

    Distractify

    1 Aralık 2011
  • infodirt

    infodirt

    11 Mart 2009
  • wowchick16

    wowchick16

    17 Mart 2007