from datetime import date
import time
# create date
d = date.fromisoformat('2019-12-04')
d = date(2021, 2, 5)
d = date.today()
d = date.fromtimestamp(time.time())
# change date
temp = d.replace(day = 10)
temp = d.replace(month = 10)
temp = d.replace(year = 2022)
print(temp)
# access
d.year # int
d.month # int
d.day # int
d.timetuple() # year, month, day, hour, min, sec, d.weekday, d.day, -1
d.weekday() # Monday is 0 and Sunday is 6
d.isoweekday() # Monday is 1 and Sunday is 7
d.isocalendar() # year, week and weekday
d.isoformat() # str
d.ctime() # str
d.strftime("%d/%m/%y")
2022-02-05
'05/02/21'
from datetime import datetime
# create datetime
d = datetime.today()
d = datetime.now()
d = datetime.utcnow()
d = datetime.fromtimestamp(time.time())
d = datetime.fromisoformat('2011-11-04T00:05:23')
# change date
temp = d.replace(year=2022)
# access
day = d.date() # get date
d.year # int
d.month
d.day
d.hour
d.minute
d.second
d.microsecond
d.tzinfo
d.isoformat()
d.ctime()
d.strftime("%d/%m/%y %H:%M")
'04/11/11 00:05'
d1 = datetime.fromisoformat('2011-11-04T00:05:23')
d2 = datetime.now() # 2021-02-06 00:09:26.719018
# create teimdelta
delta = d2-d1
# access
delta.days # int, day difference
delta.seconds # int, second difference
328
Reference