from datetime import datetime
from datetime import date
from datetime import time
# datetime 子类:now()
now = datetime.now()print(f'当前日期时间:{now}')print(f'当前日期:{now.date()}')print(f'当前时间:{now.time()}')print('--------------------------')# date 子类:today()
today = date.today()print(f'当前日期:{today}')print(f'当前年:{today.year}')print(f'当前月:{today.month}')print(f'当前日:{today.day}')print('--------------------------')# time 子类:
t = time(20,13,14)print(f'当前时间:{t}')print(f'当前时:{t.hour}')print(f'当前分:{t.minute}')print(f'当前秒:{t.second}')print(f'当前微妙: {t.microsecond}')print(f'当前时区:{t.tzinfo}')
from datetime import datetime
from datetime import date
from datetime import time
dt = datetime(2023,12,8,20,13,14)
d = date(2023,12,8)
t = time(20,13,14)# 格式化
dt_format = dt.strftime('%Y-%m-%d %H:%M:%S')
d_format = d.strftime('%Y-%m-%d')
t_format = t.strftime('%H:%M:%S')print(f'日期时间:{dt_format}')print(f'日期:{d_format}')print(f'时间:{t_format}')