Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to compare two date/time?

I have the following two date/time which are date_time1 and date_time2 respectively:

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 (e.g. 2017-04-16 12:11:42+00:00), would like to do the following:

#    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

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?

like image 923
Jo Ko Avatar asked Oct 24 '25 23:10

Jo Ko


2 Answers

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. I think we should add timezone data to date_time1 instead of clear other tzinfo (my first example)

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

like image 59
Peter Avatar answered Oct 26 '25 11:10

Peter


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. Then your TypeError will disappear.

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.)

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. Lets change the timezone to that applicable to Sydney, Australia. Here is a list of timezones 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 %zand %Z parameters for strftime. Using %c will print it in the local time format as will %x and %X. Using 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.

like image 45
tnd Avatar answered Oct 26 '25 12:10

tnd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!