0

I have this code:

from datetime import *
surname = ""
first_name = ""
birth_date = ""
nickname = ""
class Person(object):
    def __init__(self, surname, first_name, birth_date, nickname=None):
        self.surname = surname
        self.first_name = first_name
        self.birth_date = datetime.strptime(birth_date, "%Y-%m-%d").date()
        self.nickname = nickname
        if self.nickname is None :
            self.nickname = self.surname

    def get_age(self):
        today = date.today()
        age = today.year - self.birth_date.year
        if today.month < self.birth_date.month:
            age -= 1
        elif today.month == self.birth_date.month and today.day < self.birth_date.day:
            age -= 1
        return str(age)
    def get_fullname(self):
        return self.surname + " " + self.first_name

petroff = Person("Petrov", "Petro", "1952-01-02")
print petroff.surname
print petroff.first_name
print petroff.nickname
print petroff.birth_date
print petroff.get_fullname()
print petroff.get_age()

print petroff.birth_date give me string "1952-01-02" How I can change my code, to get the value of petroff.birth_date => datetime.date(1952, 1, 2)

Win32Sector
  • 65
  • 2
  • 10
  • related: [How to print date in a regular format in Python?](http://stackoverflow.com/q/311627/4279) – jfs Mar 21 '15 at 12:27

1 Answers1

0

According to datetime documentation, __str__ (which is called by print to obtain a printable view of an object) converts content of a date() to the string "1952-01-02". You can still compare date() objects as you want.

Guillaume Lemaître
  • 1,230
  • 13
  • 18