Python標準庫 datatime模組

2021-09-18 02:08:49 字數 1311 閱讀 5547

相比time模組,datetime提供了更加豐富的對時間日期處理的工具。

在datetime模組中,提供了如下四個常用的類:

datetime.time([hour[, minute[, second[, microsecond[, tzinfo]]]]]

max:最大的時間表示數值:

>>> datetime.time.max

datetime.time(23, 59, 59, 999999)

min:最小的時間表示數值

>>> datetime.time.min

datetime.time(0, 0)

resolution:時間間隔單位為分鐘

>>> datetime.time.resolution

datetime.timedelta(0, 0, 1)

strftime(format) 返回指定格式的字串。使用方式同time模組

>>> a = datetime.time(12,20,59,899)

>>> a.strftime('%h:%m:%s')

'12:20:59'

datetime是date和time的組合。

datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])
timedelta類是用來計算二個datetime物件的差值的

datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
包含如下屬性:

1、days:天數

2、microseconds:微秒數(>=0 並且 <1秒)

3、seconds:秒數(>=0 並且 <1天)

timedelta物件支援乙個方法:total_seconds()

即將時間差轉換為以秒為單位的乙個浮點數

>>>from datetime import timedelta

>>>year = timedelta(days=365)

>>>print (year.total_seconds())

31536000.0

python標準庫 時間庫

眾所皆知,每乙個程式語言都有自己的時間類庫,python也不例外用法十分簡單 最基本的類,time類 time基本函式介紹 import time print time.asctime 如果未傳入乙個tuple或乙個time struct就是使用當前的時間,返回乙個24字長的時間字串 就這個mon ...

python標準庫 os庫

os模組主要用於跟作業系統打交道 os模組常用的方法 os.getcwd 獲取當前工作目錄,即當前python指令碼工作的目錄路徑 os.chdir dirname 改變當前指令碼工作目錄 相當於shell下cd os.curdir 返回當前目錄 os.pardir 獲取當前目錄的父目錄字串名 os...

不屬於python標準庫 Python標準庫概覽

總結 這個部分講了一些常用的python庫的方法。一下子也記不住,不過基本都自己敲了 試了試。os模組 os模組介紹了一些作業系統級別的方法 os.getcwd 得到當前工作目錄 os.chdir 改變工作目錄 os.system mkdir haha 建立資料夾haha 字串正則匹配 匯入re模組...