常用日期方式
from datetime import datetime import time #python里使用time模块来获取当前的时间 print (time.strftime("%H:%M:%S")) ##24小时格式 :21:45:33 print (time.strftime("%H-%M-%S")) ##24小时格式 :21-50-01 print (time.strftime("%I:%M:%S")) ## 12小时格式 :09:45:33 print (time.strftime("%I-%M-%S")) ## 12小时格式 :09-49-33 #-------------------------------------------------------------------------- ## dd/mm/yyyy格式 print (time.strftime("%d/%m/%Y")) #:27/02/2020 print (time.strftime("%Y/%m/%d")) #:2020/02/27 print (time.strftime("%Y-%m-%d")) #:2020-02-27 #-------------------------------------------------------------------------- print (time.strftime("%Y-%m-%d-%I-%M-%S")) #:2020-02-27-09-51-02 print(type(time.strftime("%Y-%m-%d-%I-%M-%S"))) #:<class 'str'> #-------------------------------------------------------------------------- print(datetime.now()) #:2020-02-27 21:51:36.752944 print(time.asctime()) #Thu Feb 27 21:51:36 2020 #-------------------------------------------------------------------------- cur=datetime.now() print(cur.year) #:2020 print(cur.month) #:2 print(cur.day) #:27 print(cur.hour) #:21 print(cur.minute) #:56 print(cur.second) #:5 #--------------------------------------------------------------------------
执行结果:
21:57:05
21-57-05
09:57:05
09-57-05
27/02/2020
2020/02/27
2020-02-27
2020-02-27-09-57-05
<class 'str'>
2020-02-27 21:57:05.603468
Thu Feb 27 21:57:05 2020
2020
2
27
21
57
5