gpt4 book ai didi

python - 如何在python中将天转换为年和月?

转载 作者:行者123 更新时间:2023-12-03 22:48:46 27 4
gpt4 key购买 nike

如何在python中将天转换为年、月和日?
例如:如果某人是 5,538 天大。我怎样才能通过这样的年月日来显示它:15 岁 2 个月零 1 天
这是为了获取用户的输入

print "Please enter your birthday"
bd_year = int(raw_input("Year: "))
bd_month = int(raw_input("Month: "))
bd_day = int(raw_input("Day: "))

from datetime import date
now = date.today()

birthdate = date(int(bd_year), int(bd_month), int(bd_day))
age_in_days = now - birthdate

print "Your age is %s" % age_in_days
现在我需要以年月日为单位计算此人的生日

最佳答案

如何使用dateutil.relativedelta.relativedelta来自 dateutil library ?例如:

from dateutil.relativedelta import relativedelta
rdelta = relativedelta(now, birthdate)
print 'Age in years - ', rdelta.years
print 'Age in months - ', rdelta.months
print 'Age in days - ', rdelta.days

关于python - 如何在python中将天转换为年和月?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32083726/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com