本系列文章來源:<a>https://blog.ansheng.me/article/python-full-stack-way</a>
This module provides various functions to manipulate time values.
方法名 說明
time.sleep(int) 等待時(shí)間
time.time() 輸出時(shí)間戳膛薛,從1970年1月1號(hào)到現(xiàn)在用了多少秒
time.ctime() 返回當(dāng)前的系統(tǒng)時(shí)間
time.gmtime() 將時(shí)間戳轉(zhuǎn)換成struct_time格式
time.localtime() 以struct_time格式返回本地時(shí)間
time.mktime(time.localtime()) 將struct_time格式轉(zhuǎn)回成時(shí)間戳格式
time.strftime(“%Y-%m-%d %H:%M:%S”,time.gmtime()) 將struct_time格式轉(zhuǎn)成指定的字符串格式
time.strptime(“2016-01-28”,”%Y-%m-%d”) 將字符串格式轉(zhuǎn)換成struct_time格式
1.查看當(dāng)前時(shí)間
In [1]: import time
In [2]: time.time()
Out[2]: 1496287873.1998143
In [3]: time.ctime()
Out[3]: 'Thu Jun 1 11:31:19 2017'
2.返回當(dāng)前的昨天時(shí)間
In [5]: time.ctime()
Out[5]: 'Thu Jun 1 11:54:05 2017'
In [6]: time.ctime(time.time()-86640)
Out[6]: 'Wed May 31 11:50:21 2017'
3.將時(shí)間戳轉(zhuǎn)換成struct_time格式
In [7]: time.gmtime(time.time())
Out[7]: time.struct_time(tm_year=2017, tm_mon=6, tm_mday=1, tm_hour=4, tm_min=31, tm_sec=42, tm_wday=3, tm_yday=152, tm_isdst=0)
In [8]: obj = time.gmtime(time.time())
In [9]: obj.tm_year
Out[9]: 2017
In [10]: obj.tm_mon
Out[10]: 6
In [11]: "%s-%s-%s" % (obj.tm_year,obj.tm_mon,obj.tm_mday)
Out[11]: '2017-6-1'
4.格式化
In [12]: import time
In [13]: t = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) In [14]: t
Out[14]: '2017-06-01 12:58:12'