Python中的datetime包與time包包和模塊詳情_第1頁
Python中的datetime包與time包包和模塊詳情_第2頁
Python中的datetime包與time包包和模塊詳情_第3頁
Python中的datetime包與time包包和模塊詳情_第4頁
Python中的datetime包與time包包和模塊詳情_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第Python中的datetime包與time包包和模塊詳情目錄一、datetime包1.timedelta(params)得到一個時間增量對象2.timezone(timedelta)+timedelta(params)創(chuàng)建時區(qū)對象3.datetime模塊datetime.strftime(fmt)datetime時間對象轉字符串datetime.strptime(date_string,fmt)字符串轉成datetime時間對象datetime.timestamp(datetime_obj)將datetime時間對象轉換成秒級時間戳datetime.fromtimestamp(t)將秒級時間戳轉換成datetime時間對象4.使用datetime對象+timedelta(params)進行時間運算二、time包1.time.time()得到當前秒級時間戳2.time.localtime(second)將秒轉換成time時間對象3.time.strftime(fmt,time_obj)將time時間對象轉換成字符串4.time.strptime(time_string,fmt)將字符串轉換成time時間對象5.time.sleep(second)休眠second秒

一、datetime包

1.timedelta(params)得到一個時間增量對象

#coding:utf-8

fromdatetimeimporttimedelta

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

fromdatetimeimporttimedelta,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)獲取當前時間datetime對象

#coding:utf-8

fromdatetimeimporttimedelta,timezone,datetime

if__name__=='__main__':

'''

獲取當前時間,可以獲取指定時區(qū)的當前時間

datetime.now(timezone)

'''

now=datetime.now()

print(now)

#2025-02-2313:59:59.224286

print(type(now))

#class'datetime.datetime'

#設置指定時區(qū)的當前時間

print(datetime.now((timezone(timedelta(hours=9)))))

#2025-02-2314:59:59.224286+09:00

datetime.strftime(fmt)datetime時間對象轉字符串

#coding:utf-8

fromdatetimeimportdatetime

if__name__=='__main__':

'''

datetime.strftime(fmt)

將時間對象轉換成字符串

fmt:格式化標準,由格式符組成

常用格式符(年:%Y,月:%m,日:%D,時:%H,分:%M,秒:%S)

'''

now=datetime.now()

print(now.strftime('%Y-%m-%d%H:%M:%S'))

#2025-02-2314:04:24

datetime.strptime(date_string,fmt)字符串轉成datetime時間對象

#coding:utf-8

fromdatetimeimportdatetime

if__name__=='__main__':

'''

datetime.strptime(date_string,fmt)

將字符串轉換成時間對象,要求date_string的格式完全匹配fmt格式化標準

'''

time_obj=datetime.strptime('2025-2-22','%Y-%m-%d')

#datetime.strptime('2025-2-22','%Y-%m-%d%H')Errordate_string中不存在小時而fmt中要求有小時

print(datetime.strptime('2025-2-2214','%Y-%m-%d%H'))

#2025-02-2214:00:00

print(time_obj)

#2025-02-2200:00:00

print(type(time_obj))

#class'datetime.datetime'

datetime.timestamp(datetime_obj)將datetime時間對象轉換成秒級時間戳

#coding:utf-8

fromdatetimeimportdatetime

if__name__=='__main__':

'''

datetime.timestamp(datetime_obj)

datetime_obj:datetime時間對象

返回float

'''

print(datetime.timestamp(datetime.now()))

#1645598565.715

datetime.fromtimestamp(t)將秒級時間戳轉換成datetime時間對象

#coding:utf-8

fromdatetimeimportdatetime,timedelta,timezone

if__name__=='__main__':

'''

datetime.fromtimestamp(t)

t:秒級時間戳float類型

返回:datetime時間對象

'''

datetime_obj=datetime.fromtimestamp(1645598565.715)

print(datetime_obj)

#2025-02-2314:42:45.715000

print(type(datetime_obj))

#class'datetime.datetime'

4.使用datetime對象+timedelta(params)進行時間運算

#coding:utf-8

fromdatetimeimportdatetime,timedelta,timezone

if__name__=='__main__':

now=datetime.now()

fmt='%Y-%m-%d%H:%M:%S'

print(now.strftime(fmt))

#2025-02-2315:07:01

#3小時后時間

print((now+timedelta(hours=3)).strftime(fmt))

#2025-02-2318:07:01

#3小時前時間

print((now-timedelta(hours=3)).strftime(fmt))

#2025-02-2312:07:01

print((now+timedelta(hours=-3)).strftime(fmt))

#2025-02-2312:07:01

#建議timedelta的參數(shù)都使用正數(shù)(容易理解)

二、time包

1.time.time()得到當前秒級時間戳

#coding:utf-8

importtime

if__name__=='__main__':

print(time.time())

#1645667203.7236724

2.time.localtime(second)將秒轉換成time時間對象

#coding:utf-8

importtime

if__name__=='__main__':

#second不填,則默認當前的時間戳

t=time.localtime(time.time())

t2=time.localtime()

print(t)

#time.struct_time(tm_year=2025,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=2025,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時間對象轉換成字符串

#coding:utf-8

importtime

if__name__=='__main__':

"""

time.strftime(fmt,time_obj)

fmt:格式化標準參考datetime.strftime(fmt)

time_obj:time時間對象,不填默認是當前日期的time時間對象

"""

t=time.localtime(time.time()+3600)

print(time.strftime('%Y-%m-%d%H:%M:%S'))

#2025-02-2410:16:17

print(time.strftime('%Y-%m-%d%H:%M:%S',t))

#2025-02-2411:16:17

4.time.strptime(time_string,fmt)將字符串轉換成time時間對象

#coding:utf-8

importtime

if__name__=='__main__':

"""

time.strptime(time_string,fmt)

參考datetime.strptime(date_string,fmt)

time_string:時間字符串

fmt:格式化標準

"""

fmt='%Y-%m-%d%H:%M:%S'

t=time.strftime(fmt,time.localtime())

print(t)

#2025-02-2410:25:17

print(time.strptime(t,fmt))

#time.struct_time(tm_year=2025,tm_mon=2,tm_md

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論