Python日期比较未能给出正确结果,我是否遗漏了一些非常简单的东西?

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

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! Python日期比较未能给出正确结果,我是否遗漏了一些非常简单的东西?

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! Python日期比较未能给出正确结果,我是否遗漏了一些非常简单的东西?

so this is my test attempt..

import datetime

date1 = datetime.datetime.strptime(&quot;25.12.2022&quot;, &#39;%d.%m.%Y&#39;).strftime(&#39;%d.%m.%Y&#39;)
date2 = datetime.datetime.strptime(&quot;25.12.2025&quot;, &#39;%d.%m.%Y&#39;).strftime(&#39;%d.%m.%Y&#39;)
today = datetime.date.today().strftime(&#39;%d.%m.%Y&#39;)

if date1 &lt; today:
    print(&quot;OVERDUE&quot;)
else: 
    print(&quot;not overdue&quot;)
    
if date2 &lt; today:
    print(&quot;OVERDUE&quot;)
else:
    print(&quot;not overdue&quot;)

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

您的问题在于,因为您在日期时间和日期对象上调用了strftimedate1date2today不是日期对象,它们是字符串。因此,字符串"25.12.2022"不小于"06.01.2023"(即date1 < today为False)。显而易见的解决方法是删除对strftime的调用,但然后您将面临比较日期和日期时间的问题。要解决这个问题,您可以选择以下任一方法:

作为日期

将所有对象转换为日期,并以这种方式进行比较。这只是在date1date2上调用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 &lt; 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(&quot;25.12.2022&quot;, &#39;%d.%m.%Y&#39;).date()
date2 = datetime.datetime.strptime(&quot;25.12.2025&quot;, &#39;%d.%m.%Y&#39;).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(&quot;25.12.2022&quot;, &#39;%d.%m.%Y&#39;)
date2 = datetime.datetime.strptime(&quot;25.12.2025&quot;, &#39;%d.%m.%Y&#39;)
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.202206.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(&quot;2022.12.25&quot;, &#39;%Y.%m.%d&#39;).strftime(&#39;%Y.%m.%d&#39;)
date2 = datetime.datetime.strptime(&quot;2025.12.25&quot;, &#39;%Y.%m.%d&#39;).strftime(&#39;%Y.%m.%d&#39;)
today = datetime.date.today().strftime(&#39;%Y.%m.%d&#39;)

if date1 &lt; today:
    print(&quot;OVERDUE&quot;)
else: 
    print(&quot;not overdue&quot;)
    
if date2 &lt; today:
    print(&quot;OVERDUE&quot;)
else:
    print(&quot;not overdue&quot;)

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(&quot;25.12.2022&quot;, &#39;%d.%m.%Y&#39;) &lt;-- removed the extra strptime
date2 = datetime.strptime(&quot;25.12.2025&quot;, &#39;%d.%m.%Y&#39;) &lt;-- removed the extra strptime
today = datetime.today() &lt;-- removed the strptime

print(type(date1))
print(date2)
print(type(today))

if date1 &lt; today:
    print(&quot;OVERDUE&quot;)
else:
    print(&quot;not overdue&quot;)

if date2 &lt; today:
    print(&quot;OVERDUE&quot;)
else:
    print(&quot;not overdue&quot;)

Output:

> <class 'datetime.datetime'>
>
> 2025-12-25 00:00:00
>
> <class> 'datetime.datetime'>
>
> OVERDUE
>
> not overdue

huangapple
  • 本文由 发表于 2023年1月6日 13:32:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027273.html
匿名

发表评论

匿名网友

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

确定