Python6 min read
Python Datetime
Work with dates and times in Python.
Michael Brown
December 18, 2025
0.0k0
Handle dates and times.
Get Current Date/Time
```python from datetime import datetime, date, time
Current date and time now = datetime.now() print(now) # 2025-12-18 14:30:45.123456
Current date only today = date.today() print(today) # 2025-12-18 ```
Create Specific Date
```python from datetime import datetime
Create specific date birthday = datetime(1995, 6, 15) print(birthday)
Create from string date_str = "2025-12-18" date_obj = datetime.strptime(date_str, "%Y-%m-%d") print(date_obj) ```
Format Dates
```python from datetime import datetime
now = datetime.now()
Format as string formatted = now.strftime("%B %d, %Y") print(formatted) # December 18, 2025
Different formats print(now.strftime("%m/%d/%Y")) # 12/18/2025 print(now.strftime("%Y-%m-%d")) # 2025-12-18 ```
Date Arithmetic
```python from datetime import datetime, timedelta
today = datetime.now()
Add/subtract days tomorrow = today + timedelta(days=1) last_week = today - timedelta(weeks=1) next_month = today + timedelta(days=30)
print(tomorrow) ```
Remember
- Use datetime for date and time - Use date for date only - timedelta for duration
#Python#Intermediate#Datetime