python - python - 如何倒计时直到生日(两个日期之间)
问题描述
我的任务是创建一个程序来检查当前日期是否是你的生日。如果是,请打印“生日快乐!”。如果没有,则输出距离您的生日还有多少天。
我一直在努力完成这项任务,我想我已经让它工作了,但是如何从结果中删除逗号“,”和时间“0:00:00”输出?
我只希望它显示天数和天数。
输入:1989 6 21
期望的输出(在提问的时间/日期!):349 天
非:349 天,0:00:00
希望这很清楚,并提前感谢!
--
到目前为止,我有:
import datetime
today = datetime.date.today()
user_birth_year = int(input("Enter year of birth i.e. 1989: "))
user_birth_month = int(input("Enter month of birth i.e. for June enter 6: "))
user_birth_day = int(input("Enter day of birth i.e. for 21st enter 21: "))
my_birthday = datetime.date(today.year, user_birth_month, user_birth_day)
if my_birthday == today:
print("Happy Birthday!")
else:
if my_birthday < today:
my_birthday = my_birthday.replace(year=today.year + 1)
days_until_birthday = my_birthday - today
print(days_until_birthday)
else:
days_until_birthday = my_birthday - today
print(days_until_birthday)
解决方案
这包含在datetime
. 如果您只想要天数,请访问该days
属性。
else:
days_until_birthday = my_birthday - today
print(days_until_birthday.days, "days")
推荐阅读
- google-sheets - 我正在寻找一个使我的数字随机化直到它们总和在一个范围内的公式
- javascript - 带有文件夹的 Imgurr 图像上传
- matlab - 如何使用差异功能
- kubernetes - cluster-info ConfigMap 尚不包含令牌 ID“cjxj26”的 JWS 签名
- node.js - 集成nodejs api来flutter
- hive - 在 HIVE 中删除酸表失败
- google-sheets - 为什么需要在 Google Sheets 的 IFNA( ) 函数中指定#N/A?
- css - 如何将一个 div 放在另一个上(仅使用相对定位)
- android - 如何从 Android v4 Profiler 显示查看位图?
- python - Tensorflow2 错误:无法将混合类型的 Python 序列转换为张量