詳解python日期時(shí)間處理2
前篇我們稍微學(xué)習(xí)了Python中時(shí)間的獲取,這次繼續(xù)學(xué)習(xí)日期的時(shí)區(qū)轉(zhuǎn)換,格式化等等。
開(kāi)發(fā)中常用的日期操作還有哪些?
- 時(shí)區(qū)轉(zhuǎn)換顯示
- 日期格式化
- 秒數(shù) 與 日期 與 字符串的轉(zhuǎn)換
我們經(jīng)常會(huì)用到,比如全球化的業(yè)務(wù)根據(jù)不同客戶顯示不同時(shí)間(格式等)
在python 主要有下面兩個(gè)模塊涵蓋了常用日期處理
import time import calender
我們看看這兩個(gè)模塊。
時(shí)間處理中的類型轉(zhuǎn)換:struct_time vs str
Python中創(chuàng)建一個(gè)時(shí)間,具體來(lái)說(shuō)創(chuàng)建一個(gè)struct_time 需要一個(gè)9個(gè)元素的元組來(lái)構(gòu)造。
asctime 函數(shù)幫我們把這種類型的時(shí)間格式化為字符串。
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/10 22:49 上午 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: 雷學(xué)委 # @XueWeiTag: CodingDemo # @File : createtime.py # @Project : hello import time # fixed time: time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16, tm_isdst=16) the9fields = (2021, 11, 10, 22, 55, 11, 16, 16, 16) fixed = time.struct_time(the9fields) print("fixed time:", fixed) print("type:", type(fixed)) result = time.asctime(the9fields) # 類似struct_time,需要9個(gè)元素構(gòu)成的元組參數(shù)。 print("asc time:", result) print("type:", type(result)) localtime = time.localtime() print("local time:", localtime) print("type:", type(localtime)) print("asc time:", time.asctime(localtime))
運(yùn)行效果如下:
這個(gè)ticks就是從0時(shí)刻計(jì)算,至今的秒數(shù)累計(jì)。
可以隔一秒運(yùn)行這個(gè)程序,每次ticks值加上1(近似)
指定輸入來(lái)構(gòu)造時(shí)間:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/10 22:49 上午 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: 雷學(xué)委 # @XueWeiTag: CodingDemo # @File : createtime.py # @Project : hello import time #fixed time: time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16, tm_isdst=16) fixed = time.struct_time((2021, 11, 10, 22, 55, 11, 16, 16, 16)) print("fixed time:", fixed)
運(yùn)行效果如下:
時(shí)間與字符串轉(zhuǎn)換
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/10 22:49 上午 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: 雷學(xué)委 # @XueWeiTag: CodingDemo # @File : createtime2.py # @Project : hello import time sec = 3600 # 紀(jì)元開(kāi)始后的一個(gè)小時(shí)(GMT 19700101凌晨) # gmtime = time.gmtime(sec) print("gmtime:", gmtime) # GMT print("type:", type(gmtime)) print(time.strftime("%b %d %Y %H:%M:%S", gmtime)) print(time.strftime("%Y-%m-%d %H:%M:%S %Z", gmtime)) # 打印日期加上時(shí)區(qū) print("*" * 16) localtime = time.localtime(sec) print("localtime:", localtime) # 本地時(shí)間 print("type:", type(localtime)) print(time.strftime("%b %d %Y %H:%M:%S", localtime)) print(time.strftime("%Y-%m-%d %H:%M:%S %Z", localtime)) # 打印日期加上時(shí)區(qū) #試試其他格式 print(time.strftime("%D", localtime)) print(time.strftime("%T", localtime))
下面是運(yùn)行結(jié)果:
對(duì)于時(shí)間格式化函數(shù)(strftime) 它并不理會(huì)你傳入的時(shí)間(struct_time)是哪個(gè)時(shí)區(qū)的,照樣給你輸出,也是正確的。
但是我們寫程序拿數(shù)據(jù)的時(shí)候,必須把時(shí)區(qū)信息原樣返回到用戶端,或者是UI端,最后由客戶端本地時(shí)區(qū)設(shè)置進(jìn)行調(diào)整顯示。
最后看看,日期文本轉(zhuǎn)換為日期(struct_time).
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/10 7:49 上午 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: 雷學(xué)委 # @XueWeiTag: CodingDemo # @File : createtime4.py # @Project : hello import time print("strptime1:", time.strptime("Jan 01 1970 09:00:00", "%b %d %Y %H:%M:%S")) print("strptime2:", time.strptime("1970-01-01 09:00:00", "%Y-%m-%d %H:%M:%S")) print("strptime3:", time.strptime("1970-01-01 09:00:00 CST", "%Y-%m-%d %H:%M:%S %Z"))
下面是運(yùn)行結(jié)果:
總結(jié)
Python 日期處理挺多把戲的,換個(gè)格式打印/轉(zhuǎn)換,結(jié)果就不一樣了。
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Python3爬蟲里關(guān)于Splash負(fù)載均衡配置詳解
在本篇文章里小編給大家分享了關(guān)于Python3爬蟲里關(guān)于Splash負(fù)載均衡配置的相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-07-07解決python xx.py文件點(diǎn)擊完之后一閃而過(guò)的問(wèn)題
今天小編就為大家分享一篇解決python xx.py文件點(diǎn)擊完之后一閃而過(guò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Python操作MySQL數(shù)據(jù)庫(kù)的簡(jiǎn)單步驟分享
這篇文章主要給大家介紹了關(guān)于Python操作MySQL數(shù)據(jù)庫(kù)的簡(jiǎn)單步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04python使用beautifulsoup從愛(ài)奇藝網(wǎng)抓取視頻播放
這篇文章主要介紹了python使用beautifulsoup從愛(ài)奇藝網(wǎng)抓取視頻并播放示例,大家參考使用吧2014-01-01Pytorch對(duì)Himmelblau函數(shù)的優(yōu)化詳解
今天小編就為大家分享一篇Pytorch對(duì)Himmelblau函數(shù)的優(yōu)化詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02Django中ORM表的創(chuàng)建和增刪改查方法示例
這篇文章主要給大家介紹了關(guān)于Django中ORM表的創(chuàng)建和增刪改查等基本操作的方法,還給大家分享了django orm常用查詢篩選的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11