python中与deltatime相关的代码出现问题

python中与deltatime相关的代码出现问题,python,datetime,Python,Datetime,以下代码的目的是将日期转换为相应的日期。我以2017年10月2日为参考,因此继续进行。我不完全理解Delatime,猜测问题可能在于我如何使用它。希望有人能帮助我 import datetime ans = 'Y' today = datetime.date(2017, 10, 2) while ans is 'Y' or ans is 'y': d = input("Enter a date (DD/MM/YYYY)") day = int(d[:2]) month =

以下代码的目的是将日期转换为相应的日期。我以2017年10月2日为参考,因此继续进行。我不完全理解Delatime,猜测问题可能在于我如何使用它。希望有人能帮助我

import datetime
ans = 'Y'
today = datetime.date(2017, 10, 2)
while ans is 'Y' or ans is 'y':
    d = input("Enter a date (DD/MM/YYYY)")
    day = int(d[:2])
    month = int(d[3:5])
    year = int(d[6:])
    thatday = datetime.date(year, month, day)
    deltat = thatday - today
    dif = int(deltat.days/7)
    if dif is 6:
        print("Sunday")
    elif dif is 1:
        print("Tuesday")
    elif dif is 2:
        print ("Wednesday")
    elif dif is 3:
        print("Thursday")
    elif dif is 4:
        print("Friday")
    elif dif is 5:
        print("Saturday")
    else:
        print("Monday")
    ans = input("One more time, eh? (y/n)")

我开始试着帮助你,但当我真的不明白你需要什么时,我迷失了方向

基本上,我认为您需要阅读有关strftime的内容,它以特定的方式打印datetime对象:例如,datetime.strftime(“%a”)提供工作日。您还需要从字符串中读取日期的strtime,例如datetime.strtime(“31/12/1999”,%d/%m/%Y”)

请看下面修改的代码:

import datetime
ans = 'Y'
today = datetime.datetime.now().date()

while ans.title() == 'Y':
    d = input("Enter a date (DD/MM/YYYY)")
    thatday = datetime.datetime.strptime(d,"%d/%m/%Y").date()
    deltat = thatday - today # timedelta
    dif = int(deltat.days/7) # timedelta/7? What is this?
    print(thatday.strftime("%A")) # Here you get the weekday of thatday
    ans = input("One more time, eh? (y/n)")

我开始试着帮助你,但当我真的不明白你需要什么时,我迷失了方向

基本上,我认为您需要阅读有关strftime的内容,它以特定的方式打印datetime对象:例如,datetime.strftime(“%a”)提供工作日。您还需要从字符串中读取日期的strtime,例如datetime.strtime(“31/12/1999”,%d/%m/%Y”)

请看下面修改的代码:

import datetime
ans = 'Y'
today = datetime.datetime.now().date()

while ans.title() == 'Y':
    d = input("Enter a date (DD/MM/YYYY)")
    thatday = datetime.datetime.strptime(d,"%d/%m/%Y").date()
    deltat = thatday - today # timedelta
    dif = int(deltat.days/7) # timedelta/7? What is this?
    print(thatday.strftime("%A")) # Here you get the weekday of thatday
    ans = input("One more time, eh? (y/n)")

如果你只是在寻找一周中的哪一天,你也可以这样做。weekday()返回一个整数值,周一为0,周日为6

from datetime import *
weekdays = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
d = date(2017, 10, 2).weekday()
print(weekdays[d])

该日期返回星期一

如果您只是查找一周中的某一天,也可以这样做。weekday()返回一个整数值,周一为0,周日为6

from datetime import *
weekdays = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
d = date(2017, 10, 2).weekday()
print(weekdays[d])

该日期返回星期一

嗨!很高兴看到你尝试了+1、在Python中缺少一些关于时间的“基本”知识,比如函数strtime()的用法。如果你愿意,我可以提供一些。嗨!很高兴看到你尝试了+1、在Python中缺少一些关于时间的“基本”知识,比如函数strtime()的用法。如果你愿意,我可以提供一些。谢谢你的帮助。我刚刚意识到,我使用了“/”而不是“%”。谢谢你的帮助。我刚刚意识到,我用了“/”而不是“%”。我刚刚意识到我犯了一个愚蠢的错误,破坏了代码。现在已经纠正了,但是,换个角度看也没什么坏处,对吧?感谢您的评论:date(2017年10月2日)。strftime(“%a”)的作用与我在上面的示例相同,这意味着您不需要使用列表。我刚刚意识到我犯了一个空洞的错误,破坏了代码。现在已经纠正了,但是,换个角度看也没什么坏处,对吧?感谢您的评论:date(2017年10月2日)。strftime(“%a”)的作用与我在上面的示例相同,这意味着您不需要使用列表。