简体   繁体   English

Python:如何比较两个日期/时间?

[英]Python: How to compare two date/time?

I have the following two date/time which are date_time1 and date_time2 respectively: 我有以下两个日期/时间,分别是date_time1date_time2

2017-04-15 00:00:00
2017-04-17 15:35:19+00:00

parsed1 = dateutil.parser.parse(date_time1)
parsed2 = dateutil.parser.parse(date_time2)

and would if I were to receive another date/time called input_date_time (eg 2017-04-16 12:11:42+00:00), would like to do the following: 如果我要收到另一个名为input_date_time日期/时间(例如2017-04-16 input_date_time + 00:00),我想做以下事情:

#    Would like to check if `input_date_time` is within the range 
if parsed1 <= input_date_time <= parsed2:
… 

And got an error: TypeError: can't compare offset-naive and offset-aware datetimes 并得到一个错误: TypeError: can't compare offset-naive and offset-aware datetimes

Thought up of breaking it down to just year, month, day, hour, minute, and second, and compare every single one. 想把它分解为年,月,日,小时,分钟和秒,并比较每一个。

What would be the proper way to do so? 这样做的正确方法是什么?

here is my edited (again) example I think we should provide timezone data to every datetime object assume that date_time1 is a local time. 这是我编辑的(再次)示例我认为我们应该为每个日期时间对象提供时区数据,假设date_time1是本地时间。 I think we should add timezone data to date_time1 instead of clear other tzinfo (my first example) 我想我们应该将时区数据添加到date_time1而不是清除其他tzinfo(我的第一个例子)

import dateutil.parser
import datetime
from pytz import utc

date_time1 ='2017-04-15 00:00:00'
date_time2 ='2017-04-17 15:35:19+00:00'
input_date_time = '2017-04-16 12:11:42+00:00'

parsed1 = dateutil.parser.parse(date_time1).astimezone(utc)
parsed2 = dateutil.parser.parse(date_time2)
input_parsed = dateutil.parser.parse(input_date_time)

if parsed1 <= input_parsed  <= parsed2: 
    print('input is between')

this can check if input is between parsed1 and parsed2 这可以检查输入是否在parsed1parsed2之间

Assuming you have python datetime obejcts, two objects in python can be compared with the "<", "==", and ">" signs. 假设你有python datetime obejcts,python中的两个对象可以与“<”,“==”和“>”符号进行比较。

You don't need to parse them to compare them. 您不需要解析它们来比较它们。

if date_time1 <= input_date_time <= datetime_2:
    #do work

If you don't have datetime objects, there is also a method called datetime in the datetime class, which will allow you to create datetime objects, if you'll find that useful. 如果你没有datetime对象,那么在datetime类中还有一个名为datetime的方法,如果你发现它有用,它将允许你创建datetime对象。

You need to apply a timezone to the 'naive ' datetime object ( 2017-04-15 00:00:00 in your example) (to make it TZ aware) OR convert the 'aware' datetime object ( 2017-04-17 15:35:19+00:00 in your example) to a 'naive' object and the date you are trying to compare. 您需要将时区应用于'naive'datetime对象(在您的示例中为2017-04-15 00:00:00 )(以使其能够识别TZ)或转换'aware'datetime对象( 2017-04-17 15:35:19+00:00在你的例子中)到'天真'的对象和你想要比较的日期。 Then your TypeError will disappear. 然后你的TypeError就会消失。

Since your second date has a timezone offset of +00:00 and your input_datetime is also +00:00 , let's apply UTC to the naive first date (assuming that it's the correct timezone) and then convert it to whatever timezone you need (you can skip the conversion if UTC is correct - the comparison will now work.) 由于你的第二个日期的时区偏移量为+00:00而你的input_datetime也是+00:00 ,让我们将UTC应用于天真的第一个日期(假设它是正确的时区),然后将其转换为你需要的任何时区(你如果UTC正确,则可以跳过转换 - 现在比较将有效。)

parsed1 = dateutil.parser.parse(date_time1)
parsed2 = dateutil.parser.parse(date_time2)
# make parsed1 timezone aware (UTC)
parsed1 = parsed1.replace(tzinfo=pytz.utc)

Now your comparison should work. 现在你的比较应该有效。 If you want to apply another timezone to any of the dates, you can use the astimezone function. 如果要将其他时区应用于任何日期,可以使用astimezone函数。 Lets change the timezone to that applicable to Sydney, Australia. 让我们将时区更改为适用于澳大利亚悉尼的时区。 Here is a list of timezones https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568 这是一个时区列表https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568

syd_tz = pytz.timezone('Australia/Sydney')
syd_parsed1 = parsed1.astimezone(syd_tz)

You can now check what timezone is applied to each of your datetime objects using the %z and %Z parameters for strftime. 现在,您可以使用strftime的%z%Z参数检查每个日期时间对象应用的时区。 Using %c will print it in the local time format as will %x and %X . 使用%c将以本地时间格式打印它, %x%X Using Python3+: 使用Python3 +:

print("Local time: %s" % syd_parsed1.strftime('%c'))
print("Offset-Timezone-Date-Time: %s" % syd_parsed1.strftime("%z-%Z-%x-%X))

Hope that helps, the timezone functions did my head in when I used them the first time when I didn't know about %c . 希望有所帮助,当我第一次使用时区时,我不知道有关%c的时区功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM