python - How to convert datetime -
this question has answer here:
- converting string datetime 14 answers
i have 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 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 , 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 can change code, value of petroff.birth_date => datetime.date(1952, 1, 2)
according datetime documentation, __str__
(which called print
obtain printable view of object) converts content of date()
string "1952-01-02". can still compare date()
objects want.
Comments
Post a Comment