本篇在Python日期和時間函數(一)和(二)的基礎上,繼續講解Python日期和時間函數的知識.
在Python程序中,datetimeUsing object-oriented programming design module is a,可以在 Python 軟件
Used in the project date and time.相比於time模塊,datetimeMore intuitive interface module、More easy to call.
在模塊datetime中定義了兩個常量: datetime.MINYEAR和 datetime.MAXYEAR,分別
表示 datetime 所能表示的最小、最大年份.其中,MINYEAR=1, MAXYEAR = 9999.
在模塊datetimeDefined in the following table is shown in the class.
注意:The types of objects are immutable listed above(immutable)的.
類date表示一個日期、Yue period by years、月、日組成,其構造函數如下所示:
class datetime.date(year,month,day)
● year的范圍是[MINYEAR,MAXYEAR],即[1,9999].
● month的范圍是[1,12].月份是從1開始的,不是從0開始的.
● day的最大值根據給定的year, month參數來決定,例如閏年2月份有29天.
在類 date Defined in the commonly used method is shown in the table below and properties.
例如在下面的實例文件中,Illustrates the classdate To realize the process of date operation:
from datetime import *
import time
print('date.max:', date.max)
print('date.min:', date.min)
print('date.today():',date.today())
print('date.fromtimestamp():',date.fromtimestamp (time.time()))
執行後會輸出:
在類dateProvided in the example shown in the following table of commonly used methods and properties.
例如在下面的實例文件中,Illustrates the use of the classdateAn instance of the methods and properties to realize the process of operation date:
from datetime import *
import time
now = date(2022,7,30 )
tomorrow = now.replace(day = 31)
print('now:',now,'tomorrowe:',tomorrow)
print('timetuple():',now.timetuple())
print('weekday():',now.weekday())
print('isoweekday():',now.isoweekday())
print('isocalendar()):',now.isocalendar())
print('isoformat():',now.isoformat())
執行後會輸出:
在Pytho程序中,類dateCan also be carried out for some date overloading,It allows us to date for the following operation:
date2 = date1 + timedelta #日期加上一個間隔,返回一個新的日期對象(timedelta將在下面介紹,表示時間間隔)
date2 = date1 - timedelta #Date of every interval to,返回一個新的日期對象
timedelta = date1 - date2 #兩個日期相減,Returns an object of interval between
date1 < date2 #兩個日期進行比較
注意:When operating the date,Need to prevent date beyond the scope of it can say.
在Python程序中,類time表示時間,由時、分、秒以及微秒組成.類time的構造函數如下所示:
Class datetime.time (hour[,minute[ ,second[,microsecond[,tzinfo] ] ] )
參數說明如下表所示:
類timeThe commonly used attributes as shown below:
● time.min、time.max: time 類所能表示的最小、最大時間.其中,time.min = time(0,0,0, 0),time.max = time(23, 59, 59, 999999)
● time.resolution: 時間的最小單位,這裡是1微秒.
類timeCommonly used in the instance of the methods and properties shown in the following table:
例如在下面的實例文件中,Illustrates the use of the class timeTo realize the process of date operation.
from datetime import *
tm = time (20, 54,10)
print('tm: ',tm)
print('hour: %d,minute: %d,second: %d,microsecond: %d' % (tm.hour,tm.minute,tm.second,tm.microsecond))
tml = tm.replace(hour = 21)
print('tml:', tml)
print('isoformat():', tm.isoformat())
執行後會輸出:
在Python程序中,類 datetime是 date與time的結合體,包含date與timeAll the features of information.類datetime 的構造函數如下所示:
datetime.datetime(year, month, day[, hour[, minute[, second[,microsecond[,tzinfo] ] ] ] ] )
類 datetime各個參數的含義與 date和 timeThe constructor of the same,Readers need to pay attention to the scope of the parameter value as.
類 datetimeDefined in the class attributes and methods as shown in the list below:
例如在下面的實例文件中,Illustrates the use of the classdatetimeTo realize the process of date operation:
from datetime import *
import time
print('datetime.max:', datetime.max)
print('datetime.min:', datetime.min)
print('datetime.resolution:',datetime.resolution)
print('today():',datetime.today())
print('now():',datetime.now())
print('utcnowl():',datetime.utcnow())
print('fromtimestamp(tmstmp):',datetime.fromtimestamp(time.time()) )
print('utcfromtimestamp(tmstmp):',datetime.utcfromtimestamp(time.time()))
執行後會輸出:
因為在類datetimeProvided in the instance of the methods and properties anddate和time中的類似,So in this no longer explain these methods and properties similar to that of.