詳解python日期時間處理2
前篇我們稍微學(xué)習(xí)了Python中時間的獲取,這次繼續(xù)學(xué)習(xí)日期的時區(qū)轉(zhuǎn)換,格式化等等。
開發(fā)中常用的日期操作還有哪些?
- 時區(qū)轉(zhuǎn)換顯示
- 日期格式化
- 秒數(shù) 與 日期 與 字符串的轉(zhuǎn)換
我們經(jīng)常會用到,比如全球化的業(yè)務(wù)根據(jù)不同客戶顯示不同時間(格式等)
在python 主要有下面兩個模塊涵蓋了常用日期處理
import time import calender
我們看看這兩個模塊。
時間處理中的類型轉(zhuǎn)換:struct_time vs str
Python中創(chuàng)建一個時間,具體來說創(chuàng)建一個struct_time 需要一個9個元素的元組來構(gòu)造。
asctime 函數(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ò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)行效果如下:

這個ticks就是從0時刻計(jì)算,至今的秒數(shù)累計(jì)。
可以隔一秒運(yùn)行這個程序,每次ticks值加上1(近似)
指定輸入來構(gòu)造時間:
#!/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)行效果如下:

時間與字符串轉(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ì)元開始后的一個小時(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)) # 打印日期加上時區(qū)
print("*" * 16)
localtime = time.localtime(sec)
print("localtime:", localtime) # 本地時間
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)) # 打印日期加上時區(qū)
#試試其他格式
print(time.strftime("%D", localtime))
print(time.strftime("%T", localtime))
下面是運(yùn)行結(jié)果:

對于時間格式化函數(shù)(strftime) 它并不理會你傳入的時間(struct_time)是哪個時區(qū)的,照樣給你輸出,也是正確的。
但是我們寫程序拿數(shù)據(jù)的時候,必須把時區(qū)信息原樣返回到用戶端,或者是UI端,最后由客戶端本地時區(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 日期處理挺多把戲的,換個格式打印/轉(zhuǎn)換,結(jié)果就不一樣了。
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(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)擊完之后一閃而過的問題
今天小編就為大家分享一篇解決python xx.py文件點(diǎn)擊完之后一閃而過的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python操作MySQL數(shù)據(jù)庫的簡單步驟分享
這篇文章主要給大家介紹了關(guān)于Python操作MySQL數(shù)據(jù)庫的簡單步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
python使用beautifulsoup從愛奇藝網(wǎng)抓取視頻播放
這篇文章主要介紹了python使用beautifulsoup從愛奇藝網(wǎng)抓取視頻并播放示例,大家參考使用吧2014-01-01
Pytorch對Himmelblau函數(shù)的優(yōu)化詳解
今天小編就為大家分享一篇Pytorch對Himmelblau函數(shù)的優(yōu)化詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Django中ORM表的創(chuàng)建和增刪改查方法示例
這篇文章主要給大家介紹了關(guān)于Django中ORM表的創(chuàng)建和增刪改查等基本操作的方法,還給大家分享了django orm常用查詢篩選的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11

