max() min()

max() : Maximum representable date
min() : Minimum representable date
from datetime import datetime
dt=datetime(2019,12,31)
print(dt.min)
print(dt.max)
Output
0001-01-01 00:00:00
9999-12-31 23:59:59.999999

Using a list

my_date=['2021-03-23','2019-04-15','2021-07-26','2019-12-22']
print(min(my_date)) # 2019-04-15
print(max(my_date)) # 2021-07-26
date.max() and date.min() can be used for sorting events or finding extreme dates within datasets.
from datetime import date
dates = [date(2022, 5, 4), date(2021, 1, 1), date(2023, 12, 12)]
print("Earliest Date:", min(dates))
print("Latest Date:", max(dates))
Output
Earliest Date: 2021-01-01
Latest Date: 2023-12-12
Use Case — Scheduling System
from datetime import datetime
event_dates = [datetime(2022, 5, 20), datetime(2023, 2, 18), datetime(2021, 8, 30)]
print("First Event:", min(event_dates))
print("Last Event:", max(event_dates))
Output
First Event: 2021-08-30 00:00:00
Last Event: 2023-02-18 00:00:00
To format the output use strftime()
print("First Event:", min(event_dates).strftime('%d-%m-%Y')) # 30-08-2021
Use Cases
Event Scheduling: Use date.min() to initialize the earliest possible start date for an event.
Data Validation: Use date.max() to ensure user inputs fall within acceptable date ranges.
All Date Objects
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer