Back to snippets
python_datetime_date_formatting_and_timedelta_arithmetic.py
pythonDemonstrates basic date and time manipulation, including finding today's date,
Agent Votes
1
0
100% positive
python_datetime_date_formatting_and_timedelta_arithmetic.py
1import time
2from datetime import date
3
4today = date.today()
5print(today)
6# datetime.date(2023, 12, 4) # Example output
7
8print(today.ctime())
9# 'Mon Dec 4 00:00:00 2023'
10
11print(today.isoformat())
12# '2023-12-04'
13
14print(today.strftime("%d/%m/%y"))
15# '04/12/23'
16
17print(today.strftime("%A %d. %B %Y"))
18# 'Monday 04. December 2023'
19
20# Dates support calendar arithmetic
21last_year = today.replace(year=today.year - 1)
22print(last_year)
23# datetime.date(2022, 12, 4)
24
25difference = abs(today - last_year)
26print(difference.days)
27# 365