本篇在Python日期和時間函數(一)的基礎上,繼續講解PythonKnowledge of date and time functions.
提示:以下是本篇文章正文內容,下面案例可供參考
在 Python程序中,日歷 Calendar模塊中的常用內置函數如下所示.
返回一個多行字符串格式的year年年歷,3個月一行,間隔距離為c.每日寬度間隔為w字符.每行長度為21* W+18+2*C.lRepresents the number of rows per week.例如在下面的實例文件中,Demonstrated using the abovecalendar()The function implements the process of the almanac:
import calendar
c = calendar.calendar(2022)
print(c)
執行結果如下:
返回當前每周起始日期的設置.在默認情況下,首次載入caendar模塊時返回0,That means Monday.例如在下面的實例文件中,Demonstrated using the abovefirstweekday( )The function implements the process of setting the starting date:
import calendar
calendar.setfirstweekday(calendar.SUNDAY)
print (calendar.firstweekday())
執行結果如下:
是閏年則返回 True,否則為false.例如在下面的實例文件中,Demonstrated using the aboveisleap( )函數的過程:
import calendar
print("判斷2022Whether the year is a leap year:",calendar.isleap (2022))
print("判斷2008Whether the year is a leap year:",calendar.isleap (2008))
執行結果如下:
返回在Y1和Y2兩年之間的閏年總數.例如在下面的實例文件中,Demonstrated using the aboveleapdays( )函數的過程:
import calendar
print("Determine the sum of leap years between two years:",calendar.leapdays(2010, 2022))
執行結果如下:
返回一個多行字符串格式的year年month月日歷,兩行標題,一周一行.每日寬度間隔為w字符,每行的長度為7*w+6.LIndicates the number of rows per week.例如在下面的實例文件中,Demonstrated using the abovemonth( )函數的過程:
import calendar
m = calendar.month (2022,7)
print(m)
執行結果如下:
返回一個整數的單層嵌套列表,Each sublist is loaded with an integer representing a week,year年month月外的日期都設為0.范圍內的日子都由該月第幾日表示,從1開始.例如在下面的實例文件中,Demonstrated using the abovemonthcalendar( )函數的過程:
import calendar
print(calendar.monthcalendar(2022,7))
執行結果如下:
返回兩個整數,The first integer is the day of the week for the first day of the month,The second integer is the number of days in the month(28~ 31).例如在下面的實例文件中,Demonstrated using the abovemonthrange( )函數的過程:
import calendar
print(calendar.monthrange(2022,7))
執行結果如下:
相當於print calendar.calendar(year,w,l,c).
相當於print calendar.calendar(year,w,l,c).
設置每周的起始日期碼,0(星期一)到6(星期日).
和函數time.gmtime相反,The function is to accept a time tuple form,返回該時刻的時間辍.很多PythonPrograms are assembled with a meta9組數字處理時間,具體說明如下表所示.
This way we can define a tuple,Set in a tuple9attributes to represent the above table respectively11-1中的9種數字.例如在下面的實例文件中,Demonstrated using the abovetimegm( )函數的過程:
import calendar
print(calendar.timegm((2022,7,29,20,19,0,0,0,0))) #定義有9A tuple of group numbers
執行結果如下:
返回給定日期的日期碼,0(星期一)到 6(星期日),月份為1(1月)到12(12月).
例如在下面的實例文件中,Demonstrated using the aboveweekday()函數的過程:
import calendar
print(calendar.weekday (2022,7, 29))
執行結果如下: