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

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

系統(tǒng) 1773 0

1.簡介

在編寫代碼時(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 本地簡化星期名稱

? %A 本地完整星期名稱

? %b 本地簡化的月份名稱

? %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)本身?

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


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

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

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

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

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 林周县| 泰宁县| 盘山县| 太和县| 宁夏| 汨罗市| 道真| 玉环县| 延寿县| 文水县| 涿鹿县| 广南县| 喀什市| 渝北区| 太和县| 丰原市| 翁源县| 神池县| 红河县| 兴和县| 石首市| 刚察县| 阜新市| 日照市| 申扎县| 徐州市| 耒阳市| 白银市| 禹城市| 依安县| 大冶市| 武汉市| 合江县| 临桂县| 舒兰市| 博湖县| 神农架林区| 五指山市| 镇雄县| 大新县| 齐齐哈尔市|