Python獲取秒級時間戳與毫秒級時間戳的示例代碼
1、獲取秒級時間戳與毫秒級時間戳、微秒級時間戳
import timeimport datetime t = time.time() print (t) #原始時間數(shù)據(jù)print (int(t)) #秒級時間戳print (int(round(t * 1000))) #毫秒級時間戳print (int(round(t * 1000000))) #微秒級時間戳
1499825149.257892 #原始時間數(shù)據(jù)1499825149 #秒級時間戳,10位1499825149257#毫秒級時間戳,13位1499825149257892 #微秒級時間戳,16位
2、獲取當(dāng)前日期時間
dt = datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S’)dt_ms = datetime.datetime.now().strftime(’%Y-%m-%d %H:%M:%S.%f’) # 含微秒的日期時間,來源 比特量化print(dt)print(dt_ms)
2018-09-06 21:54:462018-09-06 21:54:46.205213
3、將日期轉(zhuǎn)為秒級時間戳
dt = ’2018-01-01 10:40:30’ts = int(time.mktime(time.strptime(dt, '%Y-%m-%d %H:%M:%S')))print (ts)
1514774430
4、將秒級時間戳轉(zhuǎn)為日期
ts = 1515774430dt = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(ts))print(dt)
2018-01-13 00:27:10
5、時間格式轉(zhuǎn)成另一種時間格式
dt = ’08/02/2019 01:00’dt_new = datetime.datetime.strptime(dt, ’%m/%d/%Y %H:%M’).strftime(’%Y-%m-%d %H:%M:%S’)print(dt_new)
2019-08-02 01:00:00
6、轉(zhuǎn)結(jié)構(gòu)體時間struct_time
ta_dt = time.strptime('2018-09-06 21:54:46', ’%Y-%m-%d %H:%M:%S’) #日期時間轉(zhuǎn)結(jié)構(gòu)體 ta_ms = time.localtime(1486188476) #時間戳轉(zhuǎn)結(jié)構(gòu)體,注意時間戳要求為int,來源 比特量化print(ta_dt)print(ta_ms)
time.struct_time(tm_year=2018, tm_mon=9, tm_mday=6, tm_hour=21, tm_min=54, tm_sec=46, tm_wday=3, tm_yday=249, tm_isdst=-1)time.struct_time(tm_year=2017, tm_mon=2, tm_mday=4, tm_hour=14, tm_min=7, tm_sec=56, tm_wday=5, tm_yday=35, tm_isdst=0)
到此這篇關(guān)于Python獲取秒級時間戳與毫秒級時間戳的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Python 秒級時間戳與毫秒級時間戳內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. html中的form不提交(排除)某些input 原創(chuàng)3. bootstrap select2 動態(tài)從后臺Ajax動態(tài)獲取數(shù)據(jù)的代碼4. 網(wǎng)頁中img圖片使用css實現(xiàn)等比例自動縮放不變形(代碼已測試)5. CSS3中Transition屬性詳解以及示例分享6. python 如何在 Matplotlib 中繪制垂直線7. vue使用moment如何將時間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時間格式8. js select支持手動輸入功能實現(xiàn)代碼9. jsp文件下載功能實現(xiàn)代碼10. 開發(fā)效率翻倍的Web API使用技巧
