Find oldest/youngest datetime object in a list

Solution 1:

Oldest:

oldest = min(datetimes)

Youngest before now:

now = datetime.datetime.now(pytz.utc)
youngest = max(dt for dt in datetimes if dt < now)

Solution 2:

Given a list of dates dates:

Max date is max(dates)

Min date is min(dates)

Solution 3:

Datetimes are comparable; so you can use max(datetimes_list) and min(datetimes_list)

Solution 4:

have u tried this :

>>> from datetime import datetime as DT
>>> l =[]
>>> l.append(DT(1988,12,12))
>>> l.append(DT(1979,12,12))
>>> l.append(DT(1979,12,11))
>>> l.append(DT(2011,12,11))
>>> l.append(DT(2022,12,11))
>>> min(l)
datetime.datetime(1979, 12, 11, 0, 0)
>>> max(l)
datetime.datetime(2022, 12, 11, 0, 0)

Solution 5:

The datetime module has its own versions of min and max as available methods. https://docs.python.org/2/library/datetime.html