日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python中時(shí)間、日期、時(shí)間戳的轉(zhuǎn)換的實(shí)現(xiàn)方法

系統(tǒng) 1696 0

1.簡(jiǎn)介

在編寫(xiě)代碼時(shí),往往涉及時(shí)間、日期、時(shí)間戳的相互轉(zhuǎn)換。

2.示例

            
# 引入模塊
import time, datetime
          

2.1 str類(lèi)型的日期轉(zhuǎn)換為時(shí)間戳

            
# 字符類(lèi)型的時(shí)間
tss1 = '2013-10-10 23:40:00'
# 轉(zhuǎn)為時(shí)間數(shù)組
timeArray = time.strptime(tss1, "%Y-%m-%d %H:%M:%S")
print timeArray   
# timeArray可以調(diào)用tm_year等
print timeArray.tm_year  # 2013
# 轉(zhuǎn)為時(shí)間戳
timeStamp = int(time.mktime(timeArray))
print timeStamp # 1381419600


# 結(jié)果如下
time.struct_time(tm_year=2013, tm_mon=10, tm_mday=10, tm_hour=23, tm_min=40, tm_sec=0, tm_wday=3, tm_yday=283, tm_isdst=-1)
2013
1381419600
          

2.2 更改str類(lèi)型日期的顯示格式

            
tss2 = "2013-10-10 23:40:00"
# 轉(zhuǎn)為數(shù)組
timeArray = time.strptime(tss2, "%Y-%m-%d %H:%M:%S")
# 轉(zhuǎn)為其它顯示格式
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
print otherStyleTime # 2013/10/10 23:40:00

tss3 = "2013/10/10 23:40:00"
timeArray = time.strptime(tss3, "%Y/%m/%d %H:%M:%S")
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print otherStyleTime # 2013-10-10 23:40:00
          

2.3 時(shí)間戳轉(zhuǎn)換為指定格式的日期

            
# 使用time
timeStamp = 1381419600
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
print otherStyleTime  # 2013--10--10 23:40:00
# 使用datetime
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")
print otherStyleTime  # 2013--10--10 15:40:00
          

2.4 獲取當(dāng)前時(shí)間并且用指定格式顯示

            
# time獲取當(dāng)前時(shí)間戳
now = int(time.time())   # 1533952277
timeArray = time.localtime(now)
print timeArray
otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
print otherStyleTime  

# 結(jié)果如下
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=11, tm_hour=9, tm_min=51, tm_sec=17, tm_wday=5, tm_yday=223, tm_isdst=0)
2018--08--11 09:51:17


# datetime獲取當(dāng)前時(shí)間,數(shù)組格式
now = datetime.datetime.now()
print now
otherStyleTime = now.strftime("%Y--%m--%d %H:%M:%S")
print otherStyleTime 

# 結(jié)果如下:
2018-08-11 09:51:17.362986
2018--08--11 09:51:17
          

通過(guò)datetime.datetime.strptime(date_string, format)將原字符串進(jìn)行時(shí)間格式匹配,并賦值給time_format,然后time_format調(diào)用strftime(format)函數(shù),輸出自己想要的格式

python中時(shí)間日期格式化符號(hào):

? %y 兩位數(shù)的年份表示(00-99)

? %Y 四位數(shù)的年份表示(0000-9999)

? %m 月份(01-12)

? %d 月內(nèi)中的一天(0-31)

? %H 24小時(shí)制小時(shí)數(shù)(0-23)

? %I 12小時(shí)制小時(shí)數(shù)(01-12)

? %M 分鐘數(shù)(00-59)

? %S 秒(00-59)

? %a 本地簡(jiǎn)化星期名稱(chēng)

? %A 本地完整星期名稱(chēng)

? %b 本地簡(jiǎn)化的月份名稱(chēng)

? %B 本地完整的月份名稱(chēng)

? %c 本地相應(yīng)的日期表示和時(shí)間表示

? %j 年內(nèi)的一天(001-366)

? %p 本地A.M.或P.M.的等價(jià)符

? %U 一年中的星期數(shù)(00-53)星期天為星期的開(kāi)始

? %w 星期(0-6),星期天為星期的開(kāi)始

? %W 一年中的星期數(shù)(00-53)星期一為星期的開(kāi)始

? %x 本地相應(yīng)的日期表示

? %X 本地相應(yīng)的時(shí)間表示

? %Z 當(dāng)前時(shí)區(qū)的名稱(chēng)

? %% %號(hào)本身?

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 上犹县| 张家港市| 万载县| 开化县| 光泽县| 沧州市| 蕲春县| 德江县| 赤峰市| 沛县| 江安县| 平凉市| 肃北| 哈巴河县| 旬邑县| 汨罗市| 蒙城县| 咸阳市| 平原县| 鄂托克旗| 根河市| 高青县| 义马市| 八宿县| 盘锦市| 云和县| 明溪县| 延津县| 县级市| 嘉祥县| 抚州市| 宁城县| 青龙| 尚义县| 宕昌县| 滦平县| 海南省| 常德市| 古浪县| 丰顺县| 汝阳县|