Python 如何比较两个日期?

Python 如何比较两个日期?,python,datetime,Python,Datetime,使用Python,我如何比较两个日期以确定哪个日期更晚 例如,我想检查当前日期是否超过我正在创建的假日日期列表中的最后一个日期,以便它将自动发送电子邮件,告诉管理员更新holiday.txt文件。使用datetime方法和操作员>> >>>过去=datetime.now()-timedelta(天=1) >>>present=datetime.now() >>>过去>>日期时间(3000,1,1)>>现在-日期时间(2000年4月4日) datetime.timedelta(4242757037

使用Python,我如何比较两个日期以确定哪个日期更晚


例如,我想检查当前日期是否超过我正在创建的假日日期列表中的最后一个日期,以便它将自动发送电子邮件,告诉管理员更新holiday.txt文件。

使用
datetime
方法和操作员
>>
>>>过去=datetime.now()-timedelta(天=1)
>>>present=datetime.now()
>>>过去<现在
真的
>>>日期时间(3000,1,1)<当前
假的
>>>现在-日期时间(2000年4月4日)
datetime.timedelta(424275703762105)
datetime.date(2011,1,1)
将返回
True

datetime.date(2011,1,1)-datetime.date(2011,1,2)
将返回
datetime.timedelta(-1)

datetime.date(2011,1,1)+datetime.date(2011,1,2)
将返回
datetime.timedelta(1)

请参阅。

使用
时间

假设初始日期为如下字符串:
date1=“31/12/2015”

date2=“01/01/2016”

您可以执行以下操作:
newdate1=time.strtime(date1,“%d/%m/%Y”)
newdate2=time.strtime(date2,“%d/%m/%Y”)
将它们转换为python的日期格式。然后,比较是显而易见的:

newdate1>newdate2
将返回
False


newdate1
将返回
True

使用
datetime
的其他答案,并且比较也仅适用于时间,没有日期

例如,要检查现在是否大于或小于上午8:00,我们可以使用:

import datetime

eight_am = datetime.time( 8,0,0 ) # Time, without a date
然后比较:

datetime.datetime.now().time() > eight_am  

将返回
True

以计算两个日期差中的天数,可按如下操作:

import datetime
import math

issuedate = datetime(2019,5,9)   #calculate the issue datetime
current_date = datetime.datetime.now() #calculate the current datetime
diff_date = current_date - issuedate #//calculate the date difference with time also
amount = fine  #you want change

if diff_date.total_seconds() > 0.0:   #its matching your condition
    days = math.ceil(diff_date.total_seconds()/86400)  #calculate days (in 
    one day 86400 seconds)
    deductable_amount = round(amount,2)*days #calclulated fine for all days

因为如果一秒钟比截止日期晚,那么我们必须使用python作为最简单的可用语言来收费,比较python中的日期非常容易。python操作符
=
非常适合datetime对象。 它们在python中都有各自的含义:

  • 表示日期早于第一个日期
  • 现在:
    打印(‘未来’)
    其他:
    打印(“当前”)
    #这将打印“过去”
    
    Er,您使用
    运算符,就像使用任何其他比较一样。@JohnMachin:您使用prototype
    int compare\u dates(void const*,void const*)
    编写一个函数,将两个参数强制转换为
    struct Date*
    ,并实现比较逻辑。对于一个Python新手来说,这可能不是很明显。@larsmans:对不起。。。。s/any_language/any_reasonal_language/和任何使用不合理语言的人都应该花几分钟仔细阅读文档,并尝试date1过去的
和现在的
之间有什么不同?我无法理解您的示例,它的结果没有意义。@Emadpres:想象一下这是手动键入的。前一行先打,现在一行再打。。。因此,首先输入过去行,因此过去<现在为真。引用文档:“如果一个比较对象是幼稚的,而另一个是已知的,则如果尝试进行订单比较,则会引发TypeError。”。对于相等性比较,原始实例永远不等于已知实例。如果两个比较者都知道,并且具有相同的tzinfo属性,则忽略公共tzinfo属性,并比较基准日期时间。如果两个比较对象都知道并且具有不同的tzinfo属性,则首先通过减去其UTC偏移量(从self.utcoffset()获得)来调整比较对象。“变量名称应为
pass
pass\u,但在
之后有一点。从技术上讲,
present
在比较
pass
时也是过去的。没有回答问题。您是如何运行此代码的?金额=罚款,罚款尚未定义。。此代码将不会运行,因为…只需执行<代码>导入时间
在上述代码之前:)发布的其他答案已经涵盖了该案例。最好是寻找新的问题,以有助于该网站。
import datetime
import math

issuedate = datetime(2019,5,9)   #calculate the issue datetime
current_date = datetime.datetime.now() #calculate the current datetime
diff_date = current_date - issuedate #//calculate the date difference with time also
amount = fine  #you want change

if diff_date.total_seconds() > 0.0:   #its matching your condition
    days = math.ceil(diff_date.total_seconds()/86400)  #calculate days (in 
    one day 86400 seconds)
    deductable_amount = round(amount,2)*days #calclulated fine for all days
import datetime

date = datetime.datetime(2000, 1, 1) # Replace with whatever you want
now = datetime.datetime.now() # You can even find the current date and time using this expression

if date < now:
    print('past')
elif date > now:
    print('future')
else:
    print('present')
# This would print "past"