How to work with datetime in Python
How to get the current date in Python
from datetime import datetime
date_today = datetime.now()
print(date_today)
How to convert the current date to a timestamp in Python
from datetime import datetime
date_today = datetime.now()
timestamp = date_today.timestamp()
print(timestamp)
How to convert a timestamp back to a datetime in Python
from datetime import datetime
timestamp = 1672531199
date_from_timestamp = datetime.fromtimestamp(timestamp)
print(date_from_timestamp)
How to add an interval to a datetime in Python
from datetime import datetime, timedelta
date_today = datetime.now()
new_date = date_today + timedelta(days=5)
print(new_date)
How to format a date with time in Python
from datetime import datetime
date_today = datetime.now()
formatted_date_with_time = date_today.strftime('%Y-%m-%d %H:%M:%S')
print(formatted_date_with_time)
How to format a date without time in Python
from datetime import datetime
date_today = datetime.now()
formatted_date_without_time = date_today.strftime('%Y-%m-%d')
print(formatted_date_without_time)
Where to go next: