Python Calendar Module
1. calendar.
TextCalendar
(firstweekday=0)This class can be used to generate plain text calendars.
The Text Calendar instance have the following methods.
1.1 formatmonth( theyear , themonth ,w =0 , l =0 )
Return a month’s calendar in a multi-line string. If w is provided, it specifies the width of the date columns, which are centered. If l is given, it specifies the number of lines that each week will use. Depends on the first weekday as specified in the constructor or set by the setfirstweekday()
method.
Code to illustrate :
#Dynamic Coding Calendar module
import calendar
c = calendar.TextCalendar()
c.setfirstweekday(0) # for monday
data = c.formatmonth(2020,7)
print(data)
formatmonth()
.
#Dynamic Coding Calendar module
import calendar
c = calendar.TextCalendar()
c.setfirstweekday(0) # for monday or use calendar.Monday instead of 0
data = c.prmonth(2020,7)
print(data)
formatyear
(theyear, w=2, l=1, c=6, m=3)setfirstweekday()
method. The earliest year for which a calendar can be generated is platform-dependent.pryear
(theyear, w=2, l=1, c=6, m=3)formatyear()
.2. calendar.
HTMLCalendar
(firstweekday=0)This class can be used to generate HTML calendars.
HTMLCalendar
instances have the following methods:
2.1 formatmonth
(theyear, themonth, withyear=True)Return a month’s calendar as an HTML table. If withyear is true the year will be included in the header, otherwise just the month name will be used.
2.2 formatyear
(theyear, width=3)Return a year’s calendar as an HTML table. width (defaulting to 3) specifies the number of months per row.
2.3 formatyearpage
(theyear, width=3, css='calendar.css', encoding=None)Return a year’s calendar as a complete HTML page. width (defaulting to 3) specifies the number of months per row. css is the name for the cascading style sheet to be used.
None
can be passed if no style sheet should be used. encoding specifies the encoding to be used for the output (defaulting to the system default encoding).Code to illustrate :
#Dynamic Coding Calendar module
import calendar
# Create an HTML formatted calendar
hc = calendar.HTMLCalendar(calendar.THURSDAY)
str = hc.formatmonth(2025, 1)
print(str)
1. calendar.
firstweekday
()Returns the current setting for the weekday to start each week.
- 2.
Returns
True
if year is a leap year, otherwiseFalse
.
calendar.
isleap
(year)- 3.
Returns the number of leap years in the range from y1 to y2 (exclusive), where y1 and y2 are years.
This function works for ranges spanning a century change.
calendar.
leapdays
(y1, y2)4. calendar.
weekday
(year, month, day)Returns the day of the week (
0
is Monday) for year (1970
–…), month (1
–12
), day (1
–31
).
5. calendar.
weekheader
(n)Return a header containing abbreviated weekday names. n specifies the width in characters for one weekday.
6. calendar.
monthrange
(year, month)Returns weekday of first day of the month and number of days in month, for the specified year and month.
7. calendar.
monthcalendar
(year, month)Returns a matrix representing a month’s calendar. Each row represents a week; days outside of the month a represented by zeros. Each week begins with Monday unless set by
setfirstweekday()
.
8. calendar.
prmonth
(theyear, themonth, w=0, l=0)Prints a month’s calendar as returned by
month()
.
9. calendar.
month
(theyear, themonth, w=0, l=0)Returns a month’s calendar in a multi-line string using the
formatmonth()
of theTextCalendar
class.
10. calendar.
prcal
(year, w=0, l=0, c=6, m=3)Prints the calendar for an entire year as returned by
calendar()
.
11. calendar.
calendar
(year, w=2, l=1, c=6, m=3)Returns a 3-column calendar for an entire year as a multi-line string using the
formatyear()
of theTextCalendar
class.
12. calendar.
timegm
(tuple)An unrelated but handy function that takes a time tuple such as returned by the
gmtime()
function in thetime
module, and returns the corresponding Unix timestamp value, assuming an epoch of 1970, and the POSIX encoding. In fact,time.gmtime()
andtimegm()
are each others’ inverse.Code to illustrate :
#Dynamic Coding Calendar module
import calendar
data = calendar.month(2020,7)
print(data)
we know that we didn't code for every function and it is because if we code for every function available then the article becomes very big.
No comments