如何在 Python 中减去日期时间?
How can I subtract datetime in Python?
我正在尝试为 python 制作一个年龄计算器,但我在减去用户输入的出生日期和今天的日期时遇到了问题。我试过浮动但它不起作用。我尝试减去变量本身,但这也不起作用。
age_str = input ("Enter your birthday on dd-mm-yy Format:")```
age = datetime.datetime.strptime(age_str, '%d-%m-%Y')```
today_str = datetime.date.today()```
today = datetime.datetime.strptime(today_str, '%d-%m-%Y')```
total = age - today```
from datetime import date
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
我正在尝试为 python 制作一个年龄计算器,但我在减去用户输入的出生日期和今天的日期时遇到了问题。我试过浮动但它不起作用。我尝试减去变量本身,但这也不起作用。
age_str = input ("Enter your birthday on dd-mm-yy Format:")```
age = datetime.datetime.strptime(age_str, '%d-%m-%Y')```
today_str = datetime.date.today()```
today = datetime.datetime.strptime(today_str, '%d-%m-%Y')```
total = age - today```
from datetime import date
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))