time获取时间戳:
import time print(time.time()) E:python36python3.exe E:/pj/test/test.py 1520243808.7411149
localtime获取当前时间对象,然后再去获取年月日时分秒的属性:
import time a=time.localtime() print (a) print (a.tm_year) print (a.tm_mon) print (a.tm_mday) print (a.tm_hour) print (a.tm_min) print (a.tm_sec) E:python36python3.exe E:/pj/test/test.py time.struct_time(tm_year=2018, tm_mon=3, tm_mday=5, tm_hour=18, tm_min=4, tm_sec=43, tm_wday=0, tm_yday=64, tm_isdst=0) 2018 3 5 18 4 43
asctime获取当前时间的标准格式:
import time print(time.asctime(time.localtime())) E:python36python3.exe E:/pj/test/test.py Mon Mar 5 18:07:09 2018
strftime当前时间的格式化输出:
import time print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())) print(time.strftime('%a %b %d %H:%M:%S %Y',time.localtime())) E:python36python3.exe E:/pj/test/test.py 2018-03-05 18:08:58 Mon Mar 05 18:08:58 2018
strptime和mktime时间格式化转时间戳:
import time a=time.strftime('%a %b %d %H:%M:%S %Y',time.localtime()) print (a) print (time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))) E:python36python3.exe E:/pj/test/test.py Mon Mar 05 18:11:10 2018 1520244670.0