Python has variety of inbuilt modules. If you want to find a calendar of a given month and year, use calendar module. To find the number of days between two dates, just use "datetime" module.
Program:
To print a calendar
#First, import the calendar module
import calendar
#Create a variable “yr” for getting the year of calendar to
be displayed.
yr = int(input("Enter the year"))
#create a variable “mth” for storing the value of month.
mth = int(input("Enter the month"))
#using print function, call the function calendar.month with
two arguments year and month.
print(“The calendar for the given month and year is”,calendar.month(yr,mth))
while executing above code, the user enters the year and
month at run time.
Once the code is executed, it shows the calendar.
Next, a python program to find the number of days between
two dates is given below.
Program :
To find the number of days between two dates
This program
involves a inbuilt module “datetime”. It gets the input as two dates in numeric
format. It gives you the number of days between the two dates.
#As a beginning, import the module “datetime” and its sub
module “date”
from datetime import date
#Assign a variable fdate(first date) to some value in the
format of year, month and day.
fdate = date(2024,5,4)
#Next, Assign a variable sdate(Second date) to some value in
the format of year, month and day.
sdate = date (2024,7,25)
#Find the difference between two dates and store it into diff.
diff = (sdate - fdate).days
#Print the variable “diff” using print statement with
following message.
print(“The number of days between two days are",diff)
That’s all. Your coding is done.
Next, execute the code in the python compiler.
It displays the number of days between two dates. Here is
the output.
These are the simple python programs to display calender and finding number of days between two dates.
No comments:
Post a Comment