如何比较两个日期?

问题:如何比较两个日期?

如何使用Python比较两个日期以查看稍后的日期?

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

How would I compare two dates to see which is later, using Python?

For example, I want to check if the current date is past the last date in this list I am creating, of holiday dates, so that it will send an email automatically, telling the admin to update the holiday.txt file.


回答 0

使用datetime方法和运算符<及其种类。

>>> from datetime import datetime, timedelta
>>> past = datetime.now() - timedelta(days=1)
>>> present = datetime.now()
>>> past < present
True
>>> datetime(3000, 1, 1) < present
False
>>> present - datetime(2000, 4, 4)
datetime.timedelta(4242, 75703, 762105)

Use the datetime method and the operator < and its kin.

>>> from datetime import datetime, timedelta
>>> past = datetime.now() - timedelta(days=1)
>>> present = datetime.now()
>>> past < present
True
>>> datetime(3000, 1, 1) < present
False
>>> present - datetime(2000, 4, 4)
datetime.timedelta(4242, 75703, 762105)

回答 1

采用 time

假设您的初始日期是像这样的字符串:
date1 = "31/12/2015"
date2 = "01/01/2016"

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

newdate1 > newdate2will return False
newdate1 < newdate2will returnTrue

Use time

Let’s say you have the initial dates as strings like these:
date1 = "31/12/2015"
date2 = "01/01/2016"

You can do the following:
newdate1 = time.strptime(date1, "%d/%m/%Y") and newdate2 = time.strptime(date2, "%d/%m/%Y") to convert them to python’s date format. Then, the comparison is obvious:

newdate1 > newdate2 will return False
newdate1 < newdate2 will return True


回答 2

datetime.date(2011, 1, 1) < datetime.date(2011, 1, 2)会回来的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)

请参阅文档

datetime.date(2011, 1, 1) < datetime.date(2011, 1, 2) will return True.

datetime.date(2011, 1, 1) - datetime.date(2011, 1, 2) will return datetime.timedelta(-1).

datetime.date(2011, 1, 1) + datetime.date(2011, 1, 2) will return datetime.timedelta(1).

see the docs.


回答 3

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

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

import datetime

eight_am = datetime.time( 8,0,0 ) # Time, without a date

然后与之比较:

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

这将返回 True

Other answers using datetime and comparisons also work for time only, without a date.

For example, to check if right now it is more or less than 8:00 a.m., we can use:

import datetime

eight_am = datetime.time( 8,0,0 ) # Time, without a date

And later compare with:

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

which will return True


回答 4

要计算两个日期之间的天差,可以按照以下步骤进行:

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

因为如果在截止日期之前还有一秒钟多的时间,那么我们必须收费

For calculating days in two dates difference, can be done like below:

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

Becuase if one second is more with the due date then we have to charge