Python 時間模組time常用操作
阿新 • • 發佈:2018-10-31
time模組---->時間的獲取和轉換
time模組提供各種時間相關的功能
下面列舉一些常用的操作
獲取時間戳
timestamp = time.time() print "時間戳:",timestamp # 時間戳: 1540985031.5
獲取當地時間
localtime = time.localtime() print "當地時間",localtime # 當地時間 time.struct_time(tm_year=2018, tm_mon=10, tm_mday=31, # tm_hour=19, tm_min=24, tm_sec=41, tm_wday=2, tm_yday=304, tm_isdst=0)
時間轉換字串
print time.strftime('%Y-%m-%d %H:%M:%S',localtime) # 2018-10-31 19:26:43
語法格式
time.strftime(format,dt) dt為具體時間,datetime和date格式均可,甚至可以為時間陣列struct_time 或者 dt.strftime(format) format 可以取"%Y-%m-%d %H:%M:%S"的任意子串,來決定顯示年月日時分秒
字串轉換時間語法格式
datetime.datetime.strptime(detestr,format) datestr為字串 format 可以取"%Y-%m-%d %H:%M:%S"的任意子串,來決定生成的時間格式
參考資料:
time模組簡介和相關的專業術語
https://www.cnblogs.com/renpingsheng/p/6965044.html
https://blog.csdn.net/cm786526/article/details/79877847