import locale
from datetime import datetime,date,time
locale.setlocale(locale.LC_CTYPE, 'chinese')
print(datetime.now()) #返回當(dāng)天的日期和時(shí)間,datetime類型
today=datetime.now() #定義today為當(dāng)前日期時(shí)間對(duì)象
print(datetime.date(today)) #返回當(dāng)天的日期對(duì)象,date類型
print(datetime.time(today)) #返回當(dāng)天的時(shí)間對(duì)象,time類型
print(datetime.ctime(today)) #獲取“星期,月,日,時(shí),分,秒,年”格式的字符串
print(datetime.utcnow()) #返回當(dāng)前的UTC日期和時(shí)間,datetime類型
print(datetime.timestamp(today)) #返回當(dāng)天的時(shí)間戳(UNIX時(shí)間戳),浮點(diǎn)數(shù)類型
print(datetime.fromtimestamp(datetime.timestamp(today))) #根據(jù)時(shí)間戳返回UTC日期時(shí)間,datetime類型
date1=date(2018,2,12)
time1=time(20,53,48)
print(datetime.combine(date1,time1)) #綁定日期、時(shí)間,生成新的datetime對(duì)象
newDatetime=datetime.strptime('2021-08-03 22:53:0', '%Y-%m-%d %H:%M:%S') #使用字符串和指定格式生成新的datetime對(duì)象
print(newDatetime)
for tv in datetime.timetuple(today):
print(tv)
print(today.isocalendar()) #ISO格式的日期
print(today.strftime("%Y年%m月%d日 %H:%M:%S %p"))