Python compare time without date

Just store it however you want, then use the .time[] method of the datetime objects and make comparisons.

from datetime import datetime
dt1 = datetime[2008, 1, 1, 16, 0, 0]
dt2 = datetime[2008, 4, 7, 13, 30, 0]

print[dt1.time[] > dt2.time[]]    # True

You can also just turn your datetimes into naive datetime objects if you want to do "wall clock" arithmetic on them [as opposed to "timeline" arithmetic, which should be done with UTC datetimes]:

from dateutil import tz
from datetime import datetime
NEW_YORK = tz.gettz['America/New_York']
dt = datetime[2008, 1, 1, 16, 0, tzinfo=NEW_YORK]
dt_naive = dt.replace[tzinfo=None]   # datetime[2008, 1, 1, 16]

Introduction

When working with dates, oftentimes, you'd like to know if a given date comes before or after another date. We can get these answers by comparing dates.

In this article, we will learn how to use the Python datetime module to create and compare both naive [without timezone info] and aware [with timezone info] dates.

To compare the dates, we will use the comparison operators in Python: , ==, =, !=.

Note: The datetime module has two methods for creating dates object - datetime.datetime and datetime.date. Comparisons can only be made on objects created from the same class:

datetime.datetime.now[] >= datetime.date.today[]

This will result in a TypeError:

TypeError: can't compare datetime.datetime to datetime.date

Comparing Timezone-Naive Datetimes

Let's start off with comparing naive dates, which don't have any timezone information. First, we'll want to import the datetime module:

from datetime import datetime, date

Then, let's make a few dates that we can compare:

date1 = date[1995, 3, 20]
date2 = date[2020, 1, 1]
dob_a = datetime[1995, 3, 20]
dob_b = datetime[2020, 1, 1]

Comparing these objects is made as easy as comparing, say, integers. A date is less than another, if its time precedes the other.

In our case, date1 is considered less [ dob_b: print["person a is older than person b"] else: print["person b is older than person a"]

This results in:

person b is older than person a

Comparing Timezone-Aware Datetimes

Time zones can complicate things a bit, though, thankfully, we can apply the exact same logic to the comparison. The only difference is that we're working with aware dates - dates with additional information about the time zone they're in:

from datetime import datetime
import pytz

# Create timezone objects for different parts of the world
tz_ny= pytz.timezone['America/New_York']
tz_lon = pytz.timezone["Europe/London"]

# Year, Month, Day, Hour, Minute, Second
datetime = datetime[2010, 4, 20, 23, 30, 0]

# Localize the given date, according to the timezone objects
date_with_timezone_1 = tz_ny.localize[datetime]
date_with_timezone_2 = tz_lon.localize[datetime]

# These are now, effectively no longer the same *date* after being localized
print[date_with_timezone_1] # 2010-04-20 23:30:00-04:00
print[date_with_timezone_2] # 2010-04-20 23:30:00+01:00

print[date_with_timezone_1 == date_with_timezone_2]

Running this code results in:

False

While this statement:

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

print[date_with_timezone_1 > date_with_timezone_2]

Would result in:

True

Now, this result might strike you as a bit odd. We're comparing these two datetimes:

2010-04-20 23:30:00-04:00 # date_with_timezone_1
2010-04-20 23:30:00+01:00 # date_with_timezone_2

Intuitively, it looks like date_with_timezone_2 is indeed larger than date_with_timezone_1. Though, let's take a look at how the localize[] function works.

Here, we've used the pytz library to make our naive dates aware. We've constructed a timezone object for New York [tz_ny], and a timezone object for London [tz_lon].

Then, to inject timezone information into our datetime object, we've run the localize[] function and packed the result into date_with_timezone_1 and date_with_timezone_2.

We've put in 11:30PM into localize[], which in turn created a datetime for 11:30PM in New York, and 11:30PM in London respectively. To get from 11:30PM in New York, to 11:30PM in London, you'd have to add 4 hours. More time has passed to get New York to 11:30PM, than it took to get London to 11:30PM. Thus, the datetime corresponding to New York at 11:30PM is larger than the datetime corresponding to London at 11:30PM.

Please keep this behavior in mind when working with timezones this way.

However, it is worthy of note that comparing aware dates with naive dates will result in an error:

date_with_timezone = tz_ny.localize[datetime]
print[datetime == date_without_timezone]

This will result in the following error:

TypeError: can't compare offset-naive and offset-aware datetimes

So to compare datetime objects, both objects must both be either naive or aware.

Conclusion

In this article, we have discussed ways of comparing both timezone-aware and timezone-naive dates in Python, we have also looked at possible pitfalls that we may encounter when comparing dates and possible workarounds.

If you have any questions or contributions please leave them in the comments section below.

How do I compare only time in python?

Just call the . time[] method of the datetime objects to get their hours, minutes, seconds and microseconds. Show activity on this post. Compare their times using datetime.

How do I compare two datetime values in Python?

Use datetime. date[] to compare two dates Call datetime. date[year, month, day] twice to create two datetime. date objects representing the dates of year , month , and day . Use the built-in comparison operators [e.g. < , > , == ] to compare them.

Can you compare datetime Python?

When you have two datetime objects, the date and time one of them represent could be earlier or latest than that of other, or equal. To compare datetime objects, you can use comparison operators like greater than, less than or equal to.

How do you store time value in Python?

To get the total time stored in a timedelta object in seconds, you can use the timedelta. total_second[] method..
Subtract two datetime between them to get a timedelta object..
Add a timedelta to a datetime to get a new datetime..
Subtract a timedelta from a datetime to get a new datetime..

Chủ Đề