Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python Standard Library calendar

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use Python standard library calendar". In daily operation, I believe many people have doubts about how to use Python standard library calendar. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Python standard library calendar". Next, please follow the editor to study!

This module allows you to output calendars similar to Unix cal programs and provides other useful functions related to calendars. It is worth noting that by default, these calendars use Monday as the first day of the week and Sunday as the last day of the week (European practice). However, we can use the setfirstweekday () method to set the first day of the week to Sunday or h or other working days, and the specified date is given as an integer.

The calendar module is mainly composed of the Calendar class and some module methods, and the Calendar class derives some descendant classes to help us achieve some special functions.

Calendar

Module method

Setfirstweekday (firstweekday): specify the first day of the week, 0 is Monday, … , 6 is Sunday

Import calendar# sets Sunday as the first day of the week calendar.setfirstweekday (firstweekday=6)

Firstweekday (): returns the first day of the week, 0 is Monday, … , 6 is Sunday

Import calendar# sets Sunday as the first day of the week calendar.setfirstweekday (firstweekday=6) print (calendar.firstweekday ()) # 6

Isleap (year): determines whether the specified year is a leap year, True in a leap year and False in a normal year

Import calendar# 2018 is the average year, so it is Falseprint (calendar.isleap (2018)) # False# 2008 is the same year, so it is Trueprint (calendar.isleap (2008)) # True

Leapdays (y1, y2): returns the number of leap years between y1 and y2 years, both of which are years. Including the starting year, excluding the ending year:

Between import calendar# 2008 and 2011, only 2008 is a leap year, so the number is 1print (calendar.leapdays (2008, 2011)) # 1

Weekday (year, month, day): gets the day of the week for the specified date

Import calendar# 2018-08-08 is Wednesday. Don't forget that 2 represents Wednesday print (calendar.weekday (2018, 8, 8)) # 2

Weekheader (n): returns the abbreviation of the week, where n represents the width of the acronym

Import calendarprint (calendar.weekheader (4)) # the print result is as follows # Mon Tue Wed Thu Fri Sat Sun

Monthrange (year, month): returns a tuple of the week of the first day of the month and the number of days of the current month

Import calendar# looks at the calendar to see that 08-01 is Wednesday, and August has 31 days print (2018, 8) # (2, 31)

Monthcalendar (year, month): returns a list of days in a month (days other than the current month is 0), divided by week into a two-dimensional array. Including all dates of the week at the beginning of the month and all dates of the week at the end of the month

Import calendarcalendar.setfirstweekday (firstweekday=6) print (calendar.monthcalendar (2018, 8)) # the printed results are as follows # [[0,0,0,1,2,3,4], [5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24, 25], [26, 27, 28] 29, 30, 31, 0]]

Prmonth (theyear, themonth, Word0,0): print calendar for one month, year specified by theyear, month specified by themonth, width per cell w, default 0, internal processing done, minimum width 2ml changing l lines per column, default 0, internal processing done, at least 1 line wrapping

Import calendarcalendar.setfirstweekday (firstweekday=6) calendar.prmonth (2018, 8) # the printed result is as follows # August 2018Su Mo Tu We Th Fr Sa 12 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 23 24 2526 27 28 29 30 31

Month (theyear, themonth, wreck0,0): returns the multiline text string of the calendar for one month. Theyear specifies the year, themonth specifies the month, w each cell width, default is 0, internal processing has been done, the minimum width is 2 refill l per column, default is 0, internal processing has been done, at least 1 line wrap

Import calendarcalendar.setfirstweekday (firstweekday=6) print (calendar.month (2018, 8)) # the print result is as follows # August 2018Su Mo Tu We Th Fr Sa 12 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29 30 31

Prcal (year, 0, 1, 6, 3): print calendar for one year, w each cell width, default is 0, internal has been processed, minimum width is 2ful 1 row per column, default is 0, internal has been processed, at least 1 line is wrapped, c represents the interval width between month and month, default is 6, internal processing has been done, the minimum width is 2m to divide 12 months into m columns

Import calendarcalendar.setfirstweekday (firstweekday=6) calendar.prcal (2018 January February March AprilSu Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th 4) # the printed result is as follows: # 2018 Fr Sa Su Mo Tu We Th Fr Sa 12 3 4 5 6 12 3 12 3 12 3 4 5 6 7 7 8 9 10 11 12 13 4 5 6 7 8 9 10 4 5 6 7 8 9 10 8 9 10 11 12 13 1414 15 16 17 18 19 20 11 12 13 14 15 16 17 11 12 13 14 15 16 17 15 16 17 18 19 20 2121 22 23 24 25 26 27 18 19 20 21 22 23 24 18 19 20 21 22 23 24 22 23 24 25 26 27 2828 29 30 31 25 26 27 28 25 26 27 28 29 30 31 29 30 May June July AugustSu Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 12 3 4 5 12 12 3 4 5 6 7 12 3 4 6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 1113 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 1820 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 2527 28 29 30 31 24 25 26 27 28 29 30 29 30 31 26 27 28 29 30 31 September October November DecemberSu Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 11 2 3 4 5 6 12 3 12 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 1516 17 9 10 11 12 13 14 1516 17 18 19 20 21 22 21 2223 24 25 26 27 18 19 20 21 2223 24 16 17 18 19 20 21 2223 24 25 26 27 28 29 28 2930 31 25 26 27 28 2930 23 24 25 26 27 28 2930 30 31

Calendar (year, wag2, lumb1, center6, mbl3): returns the calendar of one year as a multi-line string, w each cell width, default 2, internal processing, minimum width is 2ml / column, l rows per column, default is 1, internal processing is done, at least 1 line wrap, c represents the width of the interval between months, default is 6, internal processing has been done, the minimum width of 2m means to divide 12 months into m columns

Import calendarcalendar.setfirstweekday (firstweekday=6) print (calendar.calendar (2018 ) # the printed result is as follows: # 2018 January February March AprilSu Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 12 3 4 5 6 12 3 12 3 12 3 4 5 6 7 7 8 9 10 11 12 13 4 5 6 7 8 9 10 4 5 6 7 8 9 10 8 9 10 11 12 13 1414 15 16 17 18 19 20 11 12 13 14 15 16 17 11 12 13 14 15 16 17 15 16 17 18 19 20 2121 22 23 24 25 26 27 18 19 20 21 22 23 24 18 19 20 21 22 23 24 22 23 24 25 26 27 2828 29 30 31 25 26 27 28 25 26 27 28 29 30 31 29 30 May June July AugustSu Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 12 3 4 5 12 12 3 4 5 6 7 12 3 4 6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 1113 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 1820 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 2527 28 29 30 31 24 25 26 27 28 29 30 29 30 31 26 27 28 29 30 31 September October November DecemberSu Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 11 2 3 4 5 6 12 3 12 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 1516 17 9 10 11 13 14 1516 17 18 19 20 21 21 2223 24 25 26 27 18 19 20 21 2223 24 16 18 19 20 21 2223 24 25 26 27 28 29 28 2930 31 25 26 27 2930 24 26 26 27 27 28 28 2930calendar.Calendar (firstweekday=0)

Firstweekday is an integer that specifies the first day of the week, with 0 being Monday (default) and 6 being Sunday

Iterweekdays (): an iterator that gets the number of a week. The first value of the iterator is the same as that of firstweekday.

From calendar import Calendarc = Calendar () print (list (c.iterweekdays () # [0,1,2,3,4,5,6]-- > stands for Monday to Sunday, 0 is Monday, that is, the first day of the week c = Calendar (firstweekday=6) print (list (c.iterweekdays () # [6,0,1,2,3,4,5]-- > represents Sunday to Saturday, 6 is Sunday, that is, the first day of the week

Itermonthdates (year, month): an iterator that gets the date of a month. This iterator returns all dates of the specified month, including all dates of the week in which the month begins and all dates of the week in which the month ends

From calendar import Calendarc = Calendar (firstweekday=6) for item in c.itermonthdates (2018, 8): print (item) # the print result is as follows # 2018-07-292018-07-302018-07-312018-08-01.. 2018-08-312018-09-01

It can be seen that 2018-07-29, 2018-07-30, 2018-07-31 are the dates of the week that begins in August, while 2018-09-01 is the date of the week that ends in August, and all the dates of August are obtained! Also notice that the date here is of type datatime.date! In addition, please set Sunday as the first day of the week, otherwise it will not be able to visually display its effect!

Itermonthdays (year, month): an iterator that returns the number of days in a month (days other than the current month is 0). This iterator returns the date number of the specified month, including all dates of the week at the beginning of the month and all dates of the week at the end of the month

From calendar import Calendarc = Calendar (firstweekday=6) for item in c.itermonthdays (2018, 8): print (item) # the printed result is as follows # 0001.. 310

Because 2018-07-29, 2018-07-30, 2018-07-31, 2018-09-01 is not the date of the specified month, so it is 0

Itermonthdays2 (year, month): an iterator that returns days and weeks of a month (days other than the current month is 0). This iterator returns a tuple of the number of days of the specified month and the number of days of the week, including all dates of the week at the beginning of the month and all dates of the week at the end of the month

From calendar import Calendarc = Calendar (firstweekday=6) for item in c.itermonthdays2 (2018, 8): print (item) # the print result is as follows # (0,6) (0,0) (0,1) (1,2)... (31,4) (0,5)

It can be seen that 2018-08-01 is Wednesday.

Monthdatescalendar (year, month): returns a list of one-month dates, divided by week, into a two-dimensional array. Including all dates of the week at the beginning of the month and all dates of the week at the end of the month

From calendar import Calendarc = Calendar (firstweekday=6) for item in c.monthdatescalendar (2018, 8): print (item) # [datetime.date (2018, 7, 29), datetime.date (2018, 7, 30), datetime.date (2018, 7, 31), datetime.date (2018, 8, 1), datetime.date (2018, 8, 2), datetime.date (2018, 8, 3) Datetime.date (2018, 8, 4)] [datetime.date (2018, 8, 5), datetime.date (2018, 8, 6), datetime.date (2018, 8, 7), datetime.date (2018, 8, 8), datetime.date (2018, 8, 9), datetime.date (2018, 8, 10), datetime.date (2018, 8, 11)] [datetime.date (2018, 8, 12), datetime.date (2018, 8, 13), datetime.date (2018,8, 14), datetime.date (2018,11) 15), datetime.date (2018, 8, 16), datetime.date (2018, 8, 17), datetime.date (2018, 8, 18)] [datetime.date (2018, 8, 19), datetime.date (2018, 8, 20), datetime.date (2018, 8, 21), datetime.date (2018, 8, 22), datetime.date (2018, 8, 23), datetime.date (2018, 8, 24), datetime.date (2018, 8, 25)] [datetime.date (2018,26), datetime.date (2018) 8, 27), datetime.date (2018, 8, 28), datetime.date (2018, 8, 29), datetime.date (2018, 8, 30), datetime.date (2018, 8, 31), datetime.date (2018, 9,1)]

As you can see, the first value printed is a list of dates for the first week, including 2018-07-29, 2018-07-30, and 2018-07-31.

Monthdayscalendar (year, month): returns a list of days in a month (days other than the current month is 0), divided by week into a two-dimensional array. Including all dates of the week at the beginning of the month and all dates of the week at the end of the month

From calendar import Calendarc = Calendar (firstweekday=6) for item in c.monthdayscalendar (2018, 8): print (item) # the print result is as follows # [0,0,0,1,2,3,4] [5, 6, 7, 8, 9, 10, 11] [12, 13, 14, 15, 16, 17, 18] [19, 20, 21, 22, 23, 24, 25] [26, 27, 28] 29, 30, 31, 0]

Monthdays2calendar (year, month): returns a list of days and weeks in a month (days other than the current month is 0), divided by week into a two-dimensional array. Including all dates of the week at the beginning of the month and all dates of the week at the end of the month

From calendar import Calendarc = Calendar (firstweekday=6) for item in c.monthdays2calendar (2018, 8): print (item) # [(0,6), (0,0), (0,1), (1,2), (2,3), (3,4), (4,5)] [(5,6), (6,0), (7) 1), (8, 2), (9, 3), (10, 4), (11, 5)] [(12, 6), (13, 0), (14, 1), (15, 2), (16, 3), (17, 4), (18, 5)] [(19, 6), (20, 0), (21, 1), (22, 2), (23, 3), (24, 4), (25) 5)] [(26,6), (27,0), (28,1), (29,2), (30,3), (31,4), (0,5)]

Yeardatescalendar (year, width=3): returns all the dates of the year as a 4-dimensional array. Width says it divides 12 months of the year, each width month into one, each containing each month, each month containing a week, and each week containing date information. Including all dates of the week at the beginning of the month and all dates of the week at the end of the month

From calendar import Calendarc = Calendar (firstweekday=6) for item in c.yeardatescalendar (2018, 3): print (item) # the print result is as follows # [[datetime.date (2017, 12, 31), datetime.date (2018, 1, 1), datetime.date (2018, 1, 2),...]] [[datetime.date (2018, 4, 1), datetime.date (2018, 4, 2) Datetime.date (2018, 4, 3),...]] [[datetime.date (2018, 7, 1), datetime.date (2018, 7, 2), datetime.date (2018, 7, 3),...]] [[datetime.date (2018, 9, 30), datetime.date (2018, 10, 1), datetime.date (2018, 10, 2),...]

Yeardayscalendar (year, width=3): returns the number of days in each month of the year, which is a 4-dimensional array. Width says it divides 12 months of the year, each width month into one; each contains each month, each month contains weeks, and the number of days per week (days other than the current month is 0). Including all dates of the week at the beginning of the month and all dates of the week at the end of the month

From calendar import Calendarc = Calendar (firstweekday=6) for item in c.yeardayscalendar (2018, 3): print (item) # the print result is as follows # [[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20] [[1, 2] 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21],...] [1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21],...] [0, 1, 2, 3, 4, 5, 6] [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20],...]

Yeardays2calendar (year, width=3): returns the tuples of days and weeks of each month for farmers in a year, which is a list of 4-dimensional tuples. Width says it divides 12 months of the year, each width month into one; each contains each month, each month contains weeks, and each week contains tuple information about the number of days and weeks (days other than the current month is 0). Including all dates of the week at the beginning of the month and all dates of the week at the end of the month

From calendar import Calendarc = Calendar (firstweekday=6) for item in c.yeardays2calendar (2018, 3): print (item) # the print result is as follows: [(0,6), (1,0), (2,1), (3,2), (4,3), (5,4), (6,5)], [(7,6), (8,0)] [[(1,6), (2,0), (3,1), (4,2), (5,3), (6,4), (7,5)], [[(1,6), (2,0), (3,1), (4,2), (5,3), (6,4), (7)] [[(1,6), (2,0), (3,1), (4,2), (5,3), (6,4), (7)] ], [[(0,6), (1,0), (2,1), (3,2), (4,3), (5,4), (6,5)], [(7,6), (8,0),...]] calendar.TextCalendar (firstweekday=0)

Calendar subclass, firstweekday is an integer, specifies the first day of the week, 0 is Monday (default), 6 is Sunday

Formatmonth (theyear, themonth, calendar 0,0): returns a calendar for a month as a multi-line string. Theyear specifies year, themonth specifies month, w each cell width, default is 0, internal processing has been done, the minimum width is 2 refill l per column, default is 0, internal processing has been done, at least 1 line wrap

From calendar import TextCalendarc = TextCalendar (firstweekday=6) print (c.formatmonth (2018, 8)) # the print result is as follows # August 2018Su Mo Tu We Th Fr Sa 12 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29 30 31

Prmonth (theyear, themonth, watt0,lumb0): prints the result of formatmonth (theyear, themonth, wreck0,lumb0), with no return value

From calendar import TextCalenadrc = TextCalendar (firstweekday=6) c.prmonth (2018, 8) # the printed result is as follows: # August 2018Su Mo Tu We Th Fr Sa 12 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29 30 31

Because there is no return value, the returned result is None

Formatyear (theyear, wag2, lumb1, center6, mbl3): returns the calendar of one year as a multi-line string, w each cell width, default 0, internal processing, minimum width is 2ml / column, l rows per column, default is 0, internal processing is done, at least 1 line wrap, interval width between c months and months, default is 6, internal processing has been done, the minimum width is 2m to divide 12 months into m columns

From calendar import TextCalendarc = TextCalendar (firstweekday=6) print (c.format (2018) ) # the printed result is as follows: # 2018 January February March AprilSu Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 12 3 4 5 6 12 3 12 3 12 3 4 5 6 7 7 8 9 10 11 12 13 4 5 6 7 8 9 10 4 5 6 7 8 9 10 8 9 10 11 12 13 1414 15 16 17 18 19 20 11 12 13 14 15 16 17 11 12 13 14 15 16 17 15 16 17 18 19 20 2121 22 23 24 25 26 27 18 19 20 21 22 23 24 18 19 20 21 22 23 24 22 23 24 25 26 27 2828 29 30 31 25 26 27 28 25 26 27 28 29 30 31 29 30 May June July AugustSu Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 12 3 4 5 12 12 3 4 5 6 7 12 3 4 6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 1113 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 1820 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 2527 28 29 30 31 24 25 26 27 28 29 30 29 30 31 26 27 28 29 30 31 September October November DecemberSu Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 11 2 3 4 5 6 12 3 12 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 1516 17 9 10 11 12 13 14 1516 17 18 19 20 21 22 21 2223 24 25 26 27 18 19 20 21 2223 24 16 17 18 19 20 21 2223 24 25 26 27 28 29 28 2930 31 25 26 27 28 2930 23 24 25 26 27 28 2930 30 31

Pryear (theyear, walled 2, lumped 1, caged 6, masked 3): returns the result of formatyear (theyear, walled 2, lumped 1, caged 6, massif 3)

Calendar.HTMLCalendar (firstweekday=0)

A subclass of Calendar, firstweekday is an integer that specifies the first day of the week, 0 is Monday (default) and 6 is Sunday

Formatmonth (theyear, themonth, withyear=Ture): returns the html content of a monthly calendar. Whether withyear displays the year or not, the default is True, that is, the year is displayed.

From calendar import HTMLCalendarc = HTMLCalendar (firstweekday=6) print (c.formatmonth (2018, 8, withyear=False)) # the print result is as follows # AugustSunMonTueWedThuFriSat 123456789101112131415161718192021222324252728293031

Formatyear (theyear, width=3): returns the html content of an one-year calendar. Width means to divide 12 months into width columns.

From calendar import HTMLCalendarc = HTMLCalendar (firstweekday=6) print (c.formatyear (2018, width=4))

Formatyearpage (theyear, width=3, css='calendar.css', encoding=None): returns the html content of an one-year calendar. Width means dividing 12 months into width columns. Css can customize css style and encoding encoding method.

From calendar import HTMLCalendarc = HTMLCalendar (firstweekday=6) print (c.formatyearpage (2018, width=4)) at this point, the study on "how to use the Python standard library calendar" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report