詳解python日期時間處理
講了很多數(shù)據(jù)容器操作,這篇我們看看時間的處理。
開發(fā)中常用的日期操作有哪些?
- 獲取當前時間
- 獲取系統(tǒng)秒數(shù)(從紀元時間開始)
- 日期跟秒數(shù)之間轉換
- 獲取日歷等
- 日期格式化顯示輸出
這些都非常常見
在python 主要有下面兩個模塊涵蓋了常用日期處理
import time import calender
我們看看這兩個模塊。
time 內(nèi)置模塊
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/10 22:49 下午 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: 雷學委 # @XueWeiTag: CodingDemo # @File : __init__.py.py # @Project : hello import time # 從19700101 零時刻開始計算經(jīng)過多少秒,精確到微秒 ticks = time.time() print("ticks=", ticks) #獲取當前時間 print(time.localtime())
運行效果如下:
這個ticks就是從0時刻計算,至今的秒數(shù)累計。
可以隔一秒運行這個程序,每次ticks值加上1(近似)
指定輸入來構造時間:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/10 22:49 上午 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: 雷學委 # @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)
運行效果如下:
calender 內(nèi)置模塊
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/10 22:49 上午 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: 雷學委 # @XueWeiTag: CodingDemo # @File : calendardemo.py # @Project : hello import calendar cal = calendar.month(2021, 11) print("cal:", cal)
至今輸出一個月份,這個在Java的Calendar中也沒有。太直接了。
日期格式化處理
這里我們使用了time模塊的strftime(str from time):
#第一個參數(shù)為格式,第二個參數(shù)為時間 time.strftime("%Y-%m-%d %H:%M:%S %Z", gmtime))
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/10 22:49 上午 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: 雷學委 # @XueWeiTag: CodingDemo # @File : createtime2.py # @Project : hello import time sec = 3600 # 紀元開始后的一個小時(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", 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", localtime)) print(time.strftime("%Y-%m-%d %H:%M:%S %Z", localtime)) # 打印日期加上時區(qū) # 試試其他格式 print(time.strftime("%D", localtime)) print(time.strftime("%T", localtime))
稍微解釋一下:
%Y-%m-%d %H:%M:%S %Z 對應的是
年份4位數(shù)-月份-日期 小時:分鐘:秒數(shù) 時區(qū)信息
%b 則是三個字母英文輸出月份,比如Jan/Feb 等。
下面是運行結果:
總結
Python 提供的日期處理都非常簡單,但是在創(chuàng)建日期方面使用time模塊沒有那么方便,需要對應元組下標才行。
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
Python用摘要算法生成token及檢驗token的示例代碼
這篇文章主要介紹了Python用摘要算法生成token及檢驗token的示例代碼,幫助大家更好的理解和學習python,感興趣的朋友可以了解下2020-12-12python re的findall和finditer的區(qū)別詳解
這篇文章主要介紹了python re的findall和finditer的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11appium+python自動化配置(adk、jdk、node.js)
這篇文章主要介紹了appium+python自動化配置(adk、jdk、node.js),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11centos 安裝python3.6環(huán)境并配置虛擬環(huán)境的詳細教程
這篇文章主要介紹了centos-安裝python3.6環(huán)境并配置虛擬環(huán)境的詳細教程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-02-02