Code Implementation
Python
from datetime import datetime, timedelta
yesterday = datetime.now() - timedelta(days=1)
print(yesterday.strftime("%Y-%m-%d"))
Get yesterday's date in Python and output it as a string.
#datetime
Snippet Description
- timedelta(days=1) means subtracting one day
- Different strftime formats can be used for output:
- %Y-%m-%d → 2025-09-30
- %d/%m/%Y → 30/09/2025
Recommended Snippets
Python Remove Duplicates from List of Dicts
Remove duplicates from a list of dictionaries based on a specific key in the dictionaries.
Python
#dict+1
6
0
Python Read Nested JSON File Example
How to read JSON files with nested structures in Python and safely access deep-level fields.
Python
#json+1
8
0
Python: Write Dict to CSV with Headers
Write a list of dictionaries to a CSV file and automatically generate headers.
Python
#dict+1
6
0
Comments
Loading...