英文:
I am trying to remove the "0:00:00". How can I do this?
问题
I am trying to remove the "0:00:00". How can I do this?
我想要移除"0:00:00",应该怎么做?
英文:
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)
I am trying to remove the "0:00:00". How can I do this?
答案1
得分: 2
你可以直接调用timedelta对象的days属性。
# datetime.timdelta对象
days_between.days
# datetime.date对象
later_date.day, earlier_date.day
英文:
You can just call the days attribute for the timedelta object
# datetime.timdelta object
days_between.days
# datetime.date object
later_date.day, earlier_date.day
答案2
得分: 1
2017年06月12日
2017年06月14日
"2017-06-12" 和 "2017-06-14" 之间有2天。
英文:
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(earlier_date)
print(later_date)
print("There are", days_between.days, "days between", earlier_date, "and", later_date)
> 2017-06-12
>
> 2017-06-14
> There are 2 days between 2017-06-12 and 2017-06-14
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论