Python Using Modules : Calendar Module

 Python Using Modules : Calendar Module

Beyond the Time module, the Calendar module can produce some interesting results when executed within your code. It does far more than simply display the date in the Time module-like format, you can actually call up a wall calendar type display.

WORKING WITH DATES

The Calendar module is built into Python 3. However, if for some reason it's not installed you can add it using pip install calendar as a Windows administrator, or sudo pip install calendar for Linux and macOS.


STEP 1


Launch Python 3 and enter: import calendar to call up the module and its inherent functions. Once it's loaded into memory, start by entering: sep=calendar. Text Calendar (calendar. SUNDAY) sep.prmonth (2019, 9)




STEP 2


You can see that the days of September 2019 are displayed in a wall calendar fashion. Naturally you can change the 2019, 9 part of the second line to any year and month you want, a birthday for example (1973, 6). The first line configures TextCalendar to start its weeks on a Sunday; you can opt for Monday if you prefer.


STEP 3


There are numerous functions within the Calendar module that may be of interest to you when forming your own code. For example, you can display the number of leap years between two specific years:

leaps=calendar. leapdays (1900, 2019) print (leaps) The result is 29, starting from 1904 onward.


STEP 4


You could even fashion that particular example into a piece of working, user interactive Python code:

import calendar

print (">>>>>>>>>>Leap Year Calculator<<<<<<<<<<<<<\n") yl=int (input ("Enter the first year: ")) y2=int(input ("Enter the second year: ")) leaps=calendar.leapdays (yl, y2) print("Number of leap years between", y1, "and", y2, "is", leaps)


STEP 5

You can also create a program that will display all the days, weeks and months within a given year: import calendar year-int (input ("Enter the year to display: ") print (calendar.prcal (year)) We're sure you'll agree that's quite a handy bit of code to have to hand.



STEP 6

Interestingly we can also list the number of days in a month by using a simple: for loop:


STEP 7

You can see that, at the outset, the code produced some zeros. This is due to the starting day of the week, Sunday in this case, plus overlapping days from the previous month. Meaning the counting of the days will start on Saturday 1st. June 2019 and will total 30, as the output correctly displays.



STEP 8

You're also able to print the individual months, or days, of the week: import calendar for name in calendar.month_name: print (name) import calendar for name in calendar.day_name: print (name)



STEP 9

import calendar cal-calendar. Text Calendar (calendar.SUNDAY) for i in cal.itermonthdays (2019, 6): print (i) daysinmonth.py -/home/pi/Documents/daysinmor Eile Edit Format Run Options Window Help import calendar cal-calendar.TextCalendar (calendar.SUNDAY) for i in cal.itermonthdays (2019, 6): print(i) The Calendar module also allows us to write the functions in HTML, so that you can display it on a website. Let's start by creating a new file: import calendar cal-open("/home/pi/Documents/cal.html", "W") c-calendar.HTMLCalendar (calendar.SUNDAY) cal.write(c. format month (2019, 1)) cal.close() This code will create an HTML file called cal, open it with a browser and it displays the calendar for January 2019.


STEP 10

Of course, you can modify that to display a given year as a web page calendar: import calendar year=int (input("Enter the year to display as a webpage: ")) cal-open("/home/pi/Documents/cal.html", "W") cal.write(calendar.HTMLCalendar (calendar. MONDAY). formatyear (year)) cal.close() This code asks the user for a year and then creates the necessary webpage. Remember to change your file destination.





Post a Comment

0 Comments