Python時間戳與日期格式之間相互轉(zhuǎn)化的詳細教程
緒論
java默認精度是毫秒級別的,生成的時間戳是13位,而python默認是10位的,精度是秒。那么python是如何生成13位時間戳,以及時間戳如何轉(zhuǎn)換為日期(年-月-日 時-分-秒)
- 13位是毫秒時間戳(難點: 輸入毫秒級的時間,轉(zhuǎn)出正常格式的時間)
- 10位是秒時間戳。
Python實現(xiàn)【時間戳】與【日期格式】之間相互轉(zhuǎn)化的應(yīng)用函數(shù)匯總表:
Python函數(shù) | 功能 | 示例 |
---|---|---|
time.time() | 獲取當前時間 | 1655179674.911647 |
int(time.time() ) | 獲取精確到秒時間戳,10位 | 1655179674 |
int(round(time.time() * 1000) ) | 獲取精確毫秒時間戳,13位 | 1655179674912 |
time.localtime (k1) | 將10位時間戳k1轉(zhuǎn)為日期格式 | time.struct_time(tm_year=2022, tm_mon=6, tm_mday=11, tm_hour=18, tm_min=19, tm_sec=48, tm_wday=5, tm_yday=162, tm_isdst=0) |
time.strftime (“%Y-%m-%d %H:%M:%S”, time.localtime (k1)) | 將10位時間戳k1轉(zhuǎn)為【年-月-日 時-分-秒】日期格式 | 2019-09-02 16:19:35 |
time.localtime(k1/1000) | 將13位時間戳k1轉(zhuǎn)為日期格式 | time.struct_time(tm_year=2022, tm_mon=6, tm_mday=11, tm_hour=18, tm_min=19, tm_sec=48, tm_wday=5, tm_yday=162, tm_isdst=0) |
time.strftime (“%Y-%m-%d %H:%M:%S”, time.localtime (k1/1000)) | 將13位時間戳k1轉(zhuǎn)為【年-月-日 時-分-秒】日期格式 | 2019-09-02 16:19:35 |
一、獲取當前日期,轉(zhuǎn)為10位或13位時間戳
- 自定義函數(shù)1 get_second():python獲取精確到秒時間戳,10位
- 自定義函數(shù)2 get_millisecond():python獲取精確毫秒時間戳,13位
- 自定義函數(shù)3 get_delta(t1,t2):兩個時間戳相減,返回秒數(shù)
# -*- coding:utf-8 -*- import time # 獲取當前日期,轉(zhuǎn)為10位時間戳格式 def get_second(): """ :return: 獲取精確到秒時間戳,10位 """ return int(time.time()) # 獲取當前日期,轉(zhuǎn)為13位時間戳格式 def get_millisecond(): """ :return: 獲取精確毫秒時間戳,13位 """ millis = int(round(time.time() * 1000)) return millis # 兩個13位的時間戳相減,返回秒數(shù) def get_delta(t1,t2): """ :param t1: 13位時間戳 :param t2: 13位時間戳 :return: 兩個時間戳相減,返回秒數(shù) """ res=int((t2 - t1)/1000) return res if __name__ == "__main__": print(get_second()) # 獲取當前時間,并轉(zhuǎn)為10位時間戳格式 >>> 1655179674 print(time.time()) # 直接打印全量精度的時間戳 >>> 1655179674.911647 time1=get_millisecond() print(time1) # 獲取當前時間,并轉(zhuǎn)為13位時間戳格式 >>> 1655179674912 # 兩個13位時間戳作差運算 k1=1567412375458 k2=1567412395853 now = int(round(time.time() * 1000)) print(now) >>> 1655179674913 t1 = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(k1/1000)) t2=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(k2/1000)) print(t1) >>> 2019-09-02 16:19:35 print(t2) >>> 2019-09-02 16:19:55 print(get_delta(k1,k2)) >>> 20
二、將10位或13位時間戳轉(zhuǎn)為日期格式(年-月-日 時-分-秒)
函數(shù)4 millisecond_to_time(millis):13位時間戳轉(zhuǎn)換為日期格式字符串
import time # 輸入毫秒級的時間,轉(zhuǎn)出正常格式的時間 def timeStamp(timeNum): timeStamp = float(timeNum/1000) timeArray = time.localtime(timeStamp) otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) print(otherStyleTime) time_st = 1654942788469 # 隨機指定時間戳 timeStamp(time_st) # 調(diào)用函數(shù) >>> 2022-06-11 18:19:48
參考鏈接:【1】在線時間轉(zhuǎn)換工具:http://tools.jb51.net/code/unixtime
總結(jié)
到此這篇關(guān)于Python時間戳與日期格式之間相互轉(zhuǎn)化的文章就介紹到這了,更多相關(guān)Python時間戳與日期格式轉(zhuǎn)化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django重裝mysql后啟動報錯:No module named ‘MySQLdb’的解決方法
這篇文章主要給大家介紹了關(guān)于Django重裝mysql后啟動報錯:No module named ‘MySQLdb’的解決方法,分享出來,對同樣遇到這個問題的朋友們一個參考學習,需要的朋友們下面隨著小編來一起學習學習吧。2018-04-04python3利用smtplib通過qq郵箱發(fā)送郵件方法示例
python實現(xiàn)郵件發(fā)送較為簡單,主要用到smtplib這個模塊,所以下面這篇文章主要給大家介紹了關(guān)于python3利用smtplib通過qq郵箱發(fā)送郵件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起看看吧。2017-12-12python清除指定目錄內(nèi)所有文件中script的方法
這篇文章主要介紹了python清除指定目錄內(nèi)所有文件中script的方法,涉及Python針對文件、字符串及正則匹配操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06