我有下面的代码,我得到的输出如下:-在2017-06-22和2017-06-27之间有5天,0:00:00天,但我只想:-在2017-06-之间有5天- 22和2017-06-27下面是我的代码:-from datetime import dateimport randomearlier_date = date(2017, 6, random.randint(1, 25))later_date = date(2017, 6, random.randint(earlier_date.day + 1, 28))days_between= later_date- earlier_dateprint("There are", days_between, "days between", earlier_date, "and", later_date)
3 回答
慕妹3242003
TA贡献1824条经验 获得超6个赞
您只需要从timedelta中提取天数
days_between = (later_date - earlier_date).days
print("There are", days_between, "days between", earlier_date, "and", later_date)
>>> There are 9 days between 2017-06-15 and 2017-06-24
您无法从timedelta获取日期的原因是因为timedelta代表日期之间的差异,而不是日期本身。问“三月和五月之间的天数是什么日期”是没有意义的。
摇曳的蔷薇
TA贡献1793条经验 获得超6个赞
试试这个。
print("There are", days_between.days, "days between", earlier_date, "and", later_date)
添加回答
举报
0/150
提交
取消