在Python中从日期时间获取日期

我有下面的代码,我得到的输出如下:-在2017-06-22和2017-06-27之间有5天,0:00:00天,但我只想:-在2017-06-之间有5天- 22和2017-06-27


下面是我的代码:-


from datetime import date

import random

earlier_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_date


print("There are", days_between, "days between", earlier_date, "and", later_date)


子衿沉夜
浏览 215回答 3
3回答

慕妹3242003

您只需要从timedelta中提取天数days_between = (later_date - earlier_date).daysprint("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代表日期之间的差异,而不是日期本身。问“三月和五月之间的天数是什么日期”是没有意义的。

摇曳的蔷薇

试试这个。print("There are", days_between.days, "days between", earlier_date, "and", later_date)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python