Python?time模塊時(shí)間獲取和轉(zhuǎn)換方法
Python time模塊時(shí)間獲取和轉(zhuǎn)換
Time模塊介紹
Python的Time庫(kù)可以進(jìn)行時(shí)間相關(guān)的處理,如訪問(wèn)當(dāng)前日期和時(shí)間,輸出不同格式的時(shí)間以及等待指定的時(shí)間等。下面介紹Python time模塊時(shí)間轉(zhuǎn)換。
1. 獲取時(shí)間
1.1. 時(shí)間戳
import time timestamp = time.time() # 1682737552.5009851
格林威治時(shí)間(GMT)1970年01月01日00時(shí)00分00秒起至現(xiàn)在的總秒數(shù)
1.2. 結(jié)構(gòu)化時(shí)間
import time struct_time = time.localtime() #time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=6, tm_sec=43, tm_wday=6, tm_yday=120, tm_isdst=0)
1.3. 格式化時(shí)間
import time
format_time = time.strftime('%Y-%m-%d %H:%M:%S %p')
# 2023-04-29 11:07:30 AM%Y Year with century as a decimal number.(年)
%m Month as a decimal number [01,12].(月)
%d Day of the month as a decimal number [01,31].(日)
%H Hour (24-hour clock) as a decimal number [00,23].(時(shí)-24時(shí))
%M Minute as a decimal number [00,59].(分)
%S Second as a decimal number [00,61].(秒)
%z Time zone offset from UTC.(國(guó)際標(biāo)準(zhǔn)時(shí)間與本地時(shí)間(東八區(qū)北京時(shí)間)的時(shí)間差)
%a Locale’s abbreviated weekday name.(星期幾-簡(jiǎn)寫(xiě))
%A Locale’s full weekday name.(星期幾-全稱)
%b Locale’s abbreviated month name.(月份名稱-簡(jiǎn)寫(xiě))
%B Locale’s full month name.(月份名稱-全稱)
%c Locale’s appropriate date and time representation.(linux的時(shí)間格式)
%I Hour (12-hour clock) as a decimal number [01,12].(時(shí)-12時(shí))
%p Locale’s equivalent of either AM or PM.(上午 or 下午)
%X 時(shí):分:秒,與%H:%M:%S效果相同
%x 時(shí)/分/秒
2. 時(shí)間轉(zhuǎn)換
2.1. 時(shí)間戳 < > 結(jié)構(gòu)化時(shí)間
import time timestamp = time.time() # 1682738174.6577663 # 時(shí)間戳 > 結(jié)構(gòu)化時(shí)間 struct_time = time.localtime(timestamp) # 默認(rèn)time.time() # time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=16, tm_sec=14, tm_wday=6, tm_yday=120, tm_isdst=0) # 結(jié)構(gòu)化時(shí)間 > 時(shí)間戳 timestamp = time.mktime(struct_time) # 1682738174.0
2.2. 結(jié)構(gòu)化時(shí)間 < > 格式化時(shí)間
import time
struct_time = time.localtime()
#time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=21, tm_sec=43, tm_wday=6, tm_yday=120, tm_isdst=0)
# 結(jié)構(gòu)化時(shí)間 > 格式化時(shí)間
format_time = time.strftime('%Y-%m-%d %H:%M:%S', struct_time)
#2023-04-29 11:21:43
# 格式化時(shí)間 > 結(jié)構(gòu)化時(shí)間
struct_time = time.strptime(format_time, '%Y-%m-%d %H:%M:%S')
#time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=21, tm_sec=43, tm_wday=6, tm_yday=120, tm_isdst=-1)到此這篇關(guān)于Python time模塊時(shí)間獲取和轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Python time模塊時(shí)間轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python編程快速上手——強(qiáng)口令檢測(cè)算法案例分析
這篇文章主要介紹了Python強(qiáng)口令檢測(cè)算法,結(jié)合實(shí)例形式分析了Python針對(duì)密碼口令強(qiáng)度檢測(cè)的相關(guān)算法實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-02-02
centos6.8安裝python3.7無(wú)法import _ssl的解決方法
這篇文章主要介紹了centos6.8安裝python3.7無(wú)法import _ssl的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
在matlab中創(chuàng)建類似字典的數(shù)據(jù)結(jié)構(gòu)方式
這篇文章主要介紹了在matlab中創(chuàng)建類似字典的數(shù)據(jù)結(jié)構(gòu)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
pytest自動(dòng)化測(cè)試中的fixture的聲明和調(diào)用
這篇文章主要為大家介紹了pytest自動(dòng)化測(cè)試中的fixture的聲明和調(diào)用,文中含有詳細(xì)示例操作有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
Python數(shù)據(jù)分析基礎(chǔ)之異常值檢測(cè)和處理方式
這篇文章主要介紹了Python數(shù)據(jù)分析基礎(chǔ)之異常值檢測(cè)和處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
tensorflow 重置/清除計(jì)算圖的實(shí)現(xiàn)
今天小編就為大家分享一篇tensorflow 重置/清除計(jì)算圖的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01

