【问题标题】:Trying to make a countdown, where you input the the date尝试倒计时,输入日期
【发布时间】:2021-03-24 13:08:46
【问题描述】:

尝试倒计时,输入日期:

from datetime import datetime

year = int(input('Enter a year: '))
month = int(input('Enter a month: '))
day = int(input('Enter a day: '))

date = datetime.date(year, month, day)

countdown = date - datetime.now()
print(countdown)

错误是:

  line 7, in <module>
    date = datetime.date(year, month, day) 
TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'int' object

【问题讨论】:

标签: python countdown


【解决方案1】:

试试这个:

date = datetime(year, month, day).date()

countdown = date - datetime.now().date()

print(countdown)

【讨论】:

    【解决方案2】:

    当操作或函数应用于不适当类型的对象时会引发该错误。

    日期时间对象是一个包含来自日期对象和时间对象的所有信息的单个对象。 日期对象表示理想化日历中的日期(年、月和日),当前的公历在两个方向上无限延伸。

    尝试将 .date() 添加到 datetime 对象。

    例如:datetime(年、月、日).date()

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多