詳解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))
運行效果如下:

這個ticks就是從0時刻計算,至今的秒數(shù)累計。
可以隔一秒運行這個程序,每次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)
運行效果如下:

時間與字符串轉(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))
下面是運行結(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"))
下面是運行結(jié)果:

總結(jié)
Python 日期處理挺多把戲的,換個格式打印/轉(zhuǎn)換,結(jié)果就不一樣了。
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
利用Python實現(xiàn)問卷星自動填寫的超詳細(xì)教程
問卷星已經(jīng)成為收集問卷的一個很重要的工具,有時可以用來報名參加活動,有時可以用來收集某些領(lǐng)域相關(guān)的情況,下面這篇文章主要給大家介紹了關(guān)于利用Python實現(xiàn)問卷星自動填寫的超詳細(xì)教程,需要的朋友可以參考下2023-06-06
Python tkinter進(jìn)度條控件(Progressbar)的使用
這篇文章主要介紹了Python tkinter進(jìn)度條控件(Progressbar)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
django數(shù)據(jù)模型(Model)的字段類型解析
這篇文章主要介紹了django數(shù)據(jù)模型(Model)的字段類型,文中給大家提到了django數(shù)據(jù)模型on_delete, db_constraint的使用,需要的朋友可以參考下2019-12-12
Python圖形化界面基礎(chǔ)篇之如何使用彈出窗口和對話框
對于Python程序員來說,處理彈出窗口似乎并不是一個常見的任務(wù),這篇文章主要給大家介紹了關(guān)于Python圖形化界面基礎(chǔ)篇之如何使用彈出窗口和對話框的相關(guān)資料,需要的朋友可以參考下2024-03-03
淺談python的dataframe與series的創(chuàng)建方法
今天小編就為大家分享一篇淺談python的dataframe與series的創(chuàng)建方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11

