Python中的datetime包與time包包和模塊詳情
更新時間:2022年02月28日 11:56:00 作者:Mar丶流年
這篇文章主要介紹了Python中的datetime包與datetime包和模塊詳情,文章圍繞主題展開詳細(xì)內(nèi)容,具有一的的參考價值,需要的小伙伴可以參考一下,希望對你有所幫助
一、datetime包
1.timedelta(params…)得到一個時間增量對象
# coding:utf-8 from datetime import timedelta if __name__ == '__main__': ? ? # 常用參數(shù) hours:小時 days:天 seconds:秒 milliseconds:毫秒 ? ? delta = timedelta(hours=2) ? ? print(delta) ?# 2:00:00 ? ? print(type(delta)) ?# <class 'datetime.timedelta'>
2.timezone(timedelta) + timedelta(params…) 創(chuàng)建時區(qū)對象
# coding:utf-8 from datetime import timedelta, timezone if __name__ == '__main__': ? ? delta = timedelta(hours=2) ? ? zone = timezone(delta) ?#配合timedelta創(chuàng)建時區(qū)對象 ? ? print(zone) ?# UTC+02:00 ? ? print(type(zone)) ?# <class 'datetime.timezone'>
3.datetime模塊
datetime.now(timezone) 獲取當(dāng)前時間datetime對象 # coding:utf-8 from datetime import timedelta, timezone, datetime if __name__ == '__main__': ? ? ''' ? ? 獲取當(dāng)前時間,可以獲取指定時區(qū)的當(dāng)前時間 ? ? datetime.now(timezone) ? ? ''' ? ? now = datetime.now() ? ? print(now) ?# 2022-02-23 13:59:59.224286 ? ? print(type(now)) ?# <class 'datetime.datetime'> ? ? # 設(shè)置指定時區(qū)的當(dāng)前時間 ? ? print(datetime.now((timezone(timedelta(hours=9))))) ?# 2022-02-23 14:59:59.224286+09:00
datetime.strftime(fmt) datetime時間對象轉(zhuǎn)字符串
# coding:utf-8
from datetime import datetime
if __name__ == '__main__':
? ? '''
? ? datetime.strftime(fmt)
? ? 將時間對象轉(zhuǎn)換成字符串
? ? fmt:格式化標(biāo)準(zhǔn),由格式符組成
? ? 常用格式符(年:%Y,月:%m,日:%D,時:%H,分:%M,秒:%S)
? ? '''
? ? now = datetime.now()
? ? print(now.strftime('%Y-%m-%d %H:%M:%S')) ?# 2022-02-23 14:04:24datetime.strptime(date_string,fmt) 字符串轉(zhuǎn)成datetime時間對象
# coding:utf-8
from datetime import datetime
if __name__ == '__main__':
? ? '''
? ? datetime.strptime(date_string,fmt)
? ? 將字符串轉(zhuǎn)換成時間對象,要求date_string的格式完全匹配fmt格式化標(biāo)準(zhǔn)
? ? '''
? ? time_obj = datetime.strptime('2022-2-22', '%Y-%m-%d')
? ? # datetime.strptime('2022-2-22', '%Y-%m-%d %H') Error date_string 中不存在小時而fmt中要求有小時
? ? print(datetime.strptime('2022-2-22 14', '%Y-%m-%d %H')) ?# 2022-02-22 14:00:00
? ? print(time_obj) ?# 2022-02-22 00:00:00
? ? print(type(time_obj)) ?# <class 'datetime.datetime'>datetime.timestamp(datetime_obj) 將datetime時間對象轉(zhuǎn)換成秒級時間戳
# coding:utf-8 from datetime import datetime if __name__ == '__main__': ? ? ''' ? ? datetime.timestamp(datetime_obj)? ? ? datetime_obj:datetime 時間對象 ? ? 返回 float ? ? ''' ? ? print(datetime.timestamp(datetime.now())) ?# 1645598565.715
datetime.fromtimestamp(t) 將秒級時間戳轉(zhuǎn)換成datetime時間對象
# coding:utf-8 from datetime import datetime, timedelta, timezone if __name__ == '__main__': ? ? ''' ? ? datetime.fromtimestamp(t) ? ? t:秒級時間戳 float類型 ? ? 返回:datetime時間對象 ? ? ''' ? ? datetime_obj = datetime.fromtimestamp(1645598565.715) ? ? print(datetime_obj) ?# 2022-02-23 14:42:45.715000 ? ? print(type(datetime_obj)) ?# <class 'datetime.datetime'>
4.使用datetime對象 + timedelta(params…) 進(jìn)行時間運(yùn)算
# coding:utf-8 from datetime import datetime, timedelta, timezone if __name__ == '__main__': ? ? now = datetime.now() ? ? fmt = '%Y-%m-%d %H:%M:%S' ? ? print(now.strftime(fmt)) ?# 2022-02-23 15:07:01 ? ? # 3小時后時間 ? ? print((now + timedelta(hours=3)).strftime(fmt)) ?# 2022-02-23 18:07:01 ? ? # 3小時前時間 ? ? print((now - timedelta(hours=3)).strftime(fmt)) ?# 2022-02-23 12:07:01 ? ? print((now + timedelta(hours=-3)).strftime(fmt)) ?# 2022-02-23 12:07:01 ? ? # 建議timedelta的參數(shù)都使用正數(shù)(容易理解)
二、time包
1.time.time() 得到當(dāng)前秒級時間戳
# coding:utf-8 import time if __name__ == '__main__': ? ? print(time.time()) ?# 1645667203.7236724
2.time.localtime(second) 將秒轉(zhuǎn)換成time時間對象
# coding:utf-8 import time if __name__ == '__main__': ? ? # second 不填,則默認(rèn)當(dāng)前的時間戳 ? ? t = time.localtime(time.time()) ? ? t2 = time.localtime() ? ? print(t) ?# time.struct_time(tm_year=2022, tm_mon=2, tm_mday=24, tm_hour=10, tm_min=10, tm_sec=8, tm_wday=3, tm_yday=55, tm_isdst=0) ? ? print(t2) ?# time.struct_time(tm_year=2022, tm_mon=2, tm_mday=24, tm_hour=10, tm_min=10, tm_sec=8, tm_wday=3, tm_yday=55, tm_isdst=0) ? ? print(type(t)) ?# <class 'time.struct_time'> ? ? print(type(t2)) ?# <class 'time.struct_time'>
3.time.strftime(fmt,time_obj) 將time時間對象轉(zhuǎn)換成字符串
# coding:utf-8
import time
if __name__ == '__main__':
? ? """
? ? time.strftime(fmt,time_obj)
? ? fmt:格式化標(biāo)準(zhǔn) 參考 datetime.strftime(fmt)
? ? time_obj:time時間對象,不填默認(rèn)是當(dāng)前日期的time時間對象
? ? """
? ? t = time.localtime(time.time() + 3600)
? ? print(time.strftime('%Y-%m-%d %H:%M:%S')) ?# 2022-02-24 10:16:17
? ? print(time.strftime('%Y-%m-%d %H:%M:%S', t)) ?# 2022-02-24 11:16:174.time.strptime(time_string,fmt) 將字符串轉(zhuǎn)換成time時間對象
# coding:utf-8 import time if __name__ == '__main__': ? ? """ ? ? time.strptime(time_string,fmt) ? ? 參考 datetime.strptime(date_string,fmt) ? ? time_string:時間字符串 ? ? fmt:格式化標(biāo)準(zhǔn) ? ? """ ? ? fmt = '%Y-%m-%d %H:%M:%S' ? ? t = time.strftime(fmt, time.localtime()) ? ? print(t) ?# 2022-02-24 10:25:17 ? ? print(time.strptime(t, fmt)) ?# time.struct_time(tm_year=2022, tm_mon=2, tm_mday=24, tm_hour=10, tm_min=25, tm_sec=40, tm_wday=3, tm_yday=55, tm_isdst=-1)
5.time.sleep(second) 休眠 second 秒
# coding:utf-8 import time if __name__ == '__main__': ? ? print(time.time()) ?# 1645670183.6567423 ? ? time.sleep(2) ? ? print(time.time()) ?# 1645670185.6708047
到此這篇關(guān)于Python中的datetime包與datetime包和模塊詳情的文章就介紹到這了,更多相關(guān)Python時間相關(guān)包和模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- Python 時間操作datetime詳情(下)
- Python?時間操作datetime詳情
- 最好的Python DateTime 庫之 Pendulum 長篇解析
- 一篇文章帶你了解python標(biāo)準(zhǔn)庫--datetime模塊
- 淺談Python3中datetime不同時區(qū)轉(zhuǎn)換介紹與踩坑
- python語言time庫和datetime庫基本使用詳解
- Python datetime 如何處理時區(qū)信息
- python 常用日期處理-- datetime 模塊的使用
- Python日期與時間模塊(datetime+time+Calendar+dateuil?)相關(guān)使用講解
相關(guān)文章
對Python 除法負(fù)數(shù)取商的取整方式詳解
今天小編就為大家分享一篇對Python 除法負(fù)數(shù)取商的取整方式詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python開發(fā)之基于模板匹配的信用卡數(shù)字識別功能
這篇文章主要介紹了基于模板匹配的信用卡數(shù)字識別功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
Django使用list對單個或者多個字段求values值實(shí)例
這篇文章主要介紹了Django使用list對單個或者多個字段求values值實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
基于python爬取梨視頻實(shí)現(xiàn)過程解析
這篇文章主要介紹了基于python爬取梨視頻實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11

