亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2019年07月06日 14:56:06   作者:火磷  
這篇文章主要介紹了python中時(shí)間、日期、時(shí)間戳的轉(zhuǎn)換的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.簡(jiǎn)介

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

2.示例

# 引入模塊
import time, datetime

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

# 字符類型的時(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類型日期的顯示格式

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

通過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)化星期名稱

  %A 本地完整星期名稱

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

  %B 本地完整的月份名稱

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

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

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

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

  %w 星期(0-6),星期天為星期的開始

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

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

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

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

  %% %號(hào)本身 

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

相關(guān)文章

最新評(píng)論