我尝试移除”0:00:00″。我该怎么做?

huangapple go评论80阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月10日 11:25:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定