How to Parse and Manipulate Dates in Python With datetime and dateutil

How to Parse and Manipulate Dates in Python With datetime and dateutil Working with dates and times in Python can get tricky, especially when dealing with multiple formats or time zones. In this tutorial, we’ll walk through how to parse, format, and manipulate dates using Python’s built-in datetime module and the powerful dateutil library. 1. Import Required Libraries Start by installing dateutil if you haven't already: pip install python-dateutil from datetime import datetime, timedelta from dateutil import parser, relativedelta 2. Parsing Date Strings Use datetime.strptime() when you know the format: date_str = "2025-04-14" parsed_date = datetime.strptime(date_str, "%Y-%m-%d") print(parsed_date) Or use dateutil.parser to auto-detect format: date_str = "April 14, 2025 10:30 AM" parsed = parser.parse(date_str) print(parsed) 3. Formatting Dates as Strings now = datetime.now() formatted = now.strftime("%B %d, %Y at %I:%M %p") print(formatted) This would output something like April 14, 2025 at 04:45 PM. 4. Performing Date Arithmetic today = datetime.today() tomorrow = today + timedelta(days=1) next_month = today + relativedelta.relativedelta(months=1) print("Tomorrow:", tomorrow) print("Next month:", next_month) 5. Comparing Dates date1 = parser.parse("2025-04-14") date2 = parser.parse("2024-12-25") if date1 > date2: print("Date1 is after Date2") 6. Handling Time Zones (Optional) For time zone-aware datetime objects, consider using pytz or zoneinfo (Python 3.9+). from datetime import timezone aware_now = datetime.now(timezone.utc) print(aware_now) Conclusion Whether you’re building a dashboard, handling user input, or scheduling jobs, being able to work with date and time data is crucial. With datetime and dateutil, you can handle parsing, formatting, and manipulating dates with ease. If this post helped you, consider supporting me: buymeacoffee.com/hexshift

Apr 16, 2025 - 06:03
 0
How to Parse and Manipulate Dates in Python With datetime and dateutil

How to Parse and Manipulate Dates in Python With datetime and dateutil

Working with dates and times in Python can get tricky, especially when dealing with multiple formats or time zones. In this tutorial, we’ll walk through how to parse, format, and manipulate dates using Python’s built-in datetime module and the powerful dateutil library.

1. Import Required Libraries

Start by installing dateutil if you haven't already:

pip install python-dateutil
from datetime import datetime, timedelta
from dateutil import parser, relativedelta

2. Parsing Date Strings

Use datetime.strptime() when you know the format:

date_str = "2025-04-14"
parsed_date = datetime.strptime(date_str, "%Y-%m-%d")
print(parsed_date)

Or use dateutil.parser to auto-detect format:

date_str = "April 14, 2025 10:30 AM"
parsed = parser.parse(date_str)
print(parsed)

3. Formatting Dates as Strings

now = datetime.now()
formatted = now.strftime("%B %d, %Y at %I:%M %p")
print(formatted)

This would output something like April 14, 2025 at 04:45 PM.

4. Performing Date Arithmetic

today = datetime.today()
tomorrow = today + timedelta(days=1)
next_month = today + relativedelta.relativedelta(months=1)

print("Tomorrow:", tomorrow)
print("Next month:", next_month)

5. Comparing Dates

date1 = parser.parse("2025-04-14")
date2 = parser.parse("2024-12-25")

if date1 > date2:
    print("Date1 is after Date2")

6. Handling Time Zones (Optional)

For time zone-aware datetime objects, consider using pytz or zoneinfo (Python 3.9+).

from datetime import timezone
aware_now = datetime.now(timezone.utc)
print(aware_now)

Conclusion

Whether you’re building a dashboard, handling user input, or scheduling jobs, being able to work with date and time data is crucial. With datetime and dateutil, you can handle parsing, formatting, and manipulating dates with ease.

If this post helped you, consider supporting me: buymeacoffee.com/hexshift