英文:
Python date comparison not giving correct result, am I missing something super simple?
问题
As part of a larger project I need to check if due dates have passed to mark tasks overdue, I'm relatively new at this so I could 100% be overlooking something glaringly obvious, but my date comparison isn't outputting the correct result. what am I missing?
Thank you in advance!
so this is my test attempt..
import datetime
date1 = datetime.datetime.strptime("25.12.2022", '%d.%m.%Y').strftime('%d.%m.%Y')
date2 = datetime.datetime.strptime("25.12.2025", '%d.%m.%Y').strftime('%d.%m.%Y')
today = datetime.date.today().strftime('%d.%m.%Y')
if date1 < today:
print("OVERDUE")
else:
print("not overdue")
if date2 < today:
print("OVERDUE")
else:
print("not overdue")
我要翻译的代码部分已经提供。
英文:
As part of a larger project I need to check if due dates have passed to mark tasks overdue, I'm relatively new at this so I could 100% be overlooking something glaringly obvious, but my date comparison isn't outputting the correct result. what am I missing?
Thank you in advance!
so this is my test attempt..
import datetime
date1 = datetime.datetime.strptime("25.12.2022", '%d.%m.%Y').strftime('%d.%m.%Y')
date2 = datetime.datetime.strptime("25.12.2025", '%d.%m.%Y').strftime('%d.%m.%Y')
today = datetime.date.today().strftime('%d.%m.%Y')
if date1 < today:
print("OVERDUE")
else:
print("not overdue")
if date2 < today:
print("OVERDUE")
else:
print("not overdue")
I intentionally set date1
to be overdue just to make sure it worked but despite that I still get the 'not overdue' option for both date1
and date2
.
答案1
得分: 2
您的问题在于,因为您在日期时间和日期对象上调用了strftime
,date1
,date2
和today
不是日期对象,它们是字符串。因此,字符串"25.12.2022"不小于"06.01.2023"(即date1 < today
为False)。显而易见的解决方法是删除对strftime
的调用,但然后您将面临比较日期和日期时间的问题。要解决这个问题,您可以选择以下任一方法:
作为日期
将所有对象转换为日期,并以这种方式进行比较。这只是在date1
和date2
上调用date()
,如下所示:
import datetime
date1 = datetime.datetime.strptime("25.12.2022", '%d.%m.%Y').date()
date2 = datetime.datetime.strptime("25.12.2025", '%d.%m.%Y').date()
today = datetime.date.today()
作为日期时间
将所有对象转换为日期时间。这只是用now
替换您对today
的调用,如下所示:
import datetime
date1 = datetime.datetime.strptime("25.12.2022", '%d.%m.%Y')
date2 = datetime.datetime.strptime("25.12.2025", '%d.%m.%Y')
today = datetime.datetime.now()
无论哪种情况,比较都将按预期工作。
英文:
Your issue is that, because you called strftime
on your datetime and date objets, date1
, date2
and today
aren't date objects, they're strings. So, the string "25.12.2022" is not less than "06.01.2023" (ie. date1 < today
is False). The obvious solution here is to remove the call to strftime
but then you'll have the issue of comparing dates and datetimes. To fix that, you can do either of the following:
As Dates
Convert all your objects to dates, and compare them that way. This just means calling date()
on date1
and date2
, like so:
import datetime
date1 = datetime.datetime.strptime("25.12.2022", '%d.%m.%Y').date()
date2 = datetime.datetime.strptime("25.12.2025", '%d.%m.%Y').date()
today = datetime.date.today()
As Datetimes
Convert all your objects to datetimes. This just means replacing your call to today
with a call to now
, like so:
import datetime
date1 = datetime.datetime.strptime("25.12.2022", '%d.%m.%Y')
date2 = datetime.datetime.strptime("25.12.2025", '%d.%m.%Y')
today = datetime.date.now()
In either case, the comparison will work as expected.
答案2
得分: 1
Code:-
import datetime
date1 = datetime.datetime.strptime("2022.12.25", '%Y.%m.%d').strftime('%Y.%m.%d')
date2 = datetime.datetime.strptime("2025.12.25", '%Y.%m.%d').strftime('%Y.%m.%d')
today = datetime.date.today().strftime('%Y.%m.%d')
if date1 < today:
print("已过期")
else:
print("未过期")
if date2 < today:
print("已过期")
else:
print("未过期")
Output:
已过期
未过期
为什么要这样做...当您比较日期、月份、年份格式时,不能保证日期始终大于年份。例如,25.12.2022
和06.01.2023
,如果您比较日期,25
会大于06
,因此输出结果是25.12.2022
> 06.01.2023
,这是不正确的!这就是为什么您应该按照年份、月份、日期的顺序进行比较的原因。
英文:
You should compare in the format [%Y%m%d]
Code:-
import datetime
date1 = datetime.datetime.strptime("2022.12.25", '%Y.%m.%d').strftime('%Y.%m.%d')
date2 = datetime.datetime.strptime("2025.12.25", '%Y.%m.%d').strftime('%Y.%m.%d')
today = datetime.date.today().strftime('%Y.%m.%d')
if date1 < today:
print("OVERDUE")
else:
print("not overdue")
if date2 < today:
print("OVERDUE")
else:
print("not overdue")
Output:
OVERDUE
not overdue
Reason you should do like this.. when you are comparing in date,month,year
format it is not guarantees that date will be greater always if the year is greater. example 25.12.2022
06.01.2023
when you comparing date 25
will be greater than 06
hence the output it gives 25.12.2022
>06.01.2023
which is not correct.! that's why you should compare in year
than month
than date
format..
答案3
得分: 0
以下是您提供的代码的翻译部分:
你可以尝试类似这样的方式。下面的比较是针对日期时间数据类型而不是字符串的:
from datetime import datetime
date1 = datetime.strptime("25.12.2022", '%d.%m.%Y') # 移除了额外的strptime
date2 = datetime.strptime("25.12.2025", '%d.%m.%Y') # 移除了额外的strptime
today = datetime.today() # 移除了strptime
print(type(date1))
print(date2)
print(type(today))
if date1 < today:
print("已过期")
else:
print("未过期")
if date2 < today:
print("已过期")
else:
print("未过期")
输出:
<class 'datetime.datetime'>
2025-12-25 00:00:00
<class 'datetime.datetime'>
已过期
未过期
请注意,我已经将代码中的双引号转换为中文标点符号,以保持代码的一致性。
英文:
You could try something like this. The below comparison is between datetime data types and not Strings:
from datetime import datetime
date1 = datetime.strptime("25.12.2022", '%d.%m.%Y') <-- removed the extra strptime
date2 = datetime.strptime("25.12.2025", '%d.%m.%Y') <-- removed the extra strptime
today = datetime.today() <-- removed the strptime
print(type(date1))
print(date2)
print(type(today))
if date1 < today:
print("OVERDUE")
else:
print("not overdue")
if date2 < today:
print("OVERDUE")
else:
print("not overdue")
Output:
> <class 'datetime.datetime'>
>
> 2025-12-25 00:00:00
>
> <class> 'datetime.datetime'>
>
> OVERDUE
>
> not overdue
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论