Open In App

Python program to print Calendar without calendar or datetime module

Last Updated : 10 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Given the month and year. The task is to show the calendar of that month and in the given year without using any module or pre-defined functions.

Examples:

Input : 
mm(1-12) :9
yy :2010

Output :    
September 2010
Su Mo Tu We Th Fr Sa
         01 02 03 04 
05 06 07 08 09 10 11 
12 13 14 15 16 17 18 
19 20 21 22 23 24 25 
26 27 28 29 30 

Approach:

In the program below, we first calculate the number of odd days to find the day of the date 01-mm-yyyy.Then, we take in the year(yy) and the month(mm) as input and displays the calendar for that month of the year.

Below is the implementation of the given approach.




# Python code to print Calendar
# Without use of Calendar module
   
mm = 2
yy = 2020
  
month ={1:'January', 2:'February', 3:'March'
        4:'April', 5:'May', 6:'June', 7:'July',
        8:'August', 9:'September', 10:'October',
        11:'November', 12:'December'}
   
# code below for calculation of odd days
day =(yy-1)% 400
day = (day//100)*5 + ((day % 100) - (day % 100)//4) + ((day % 100)//4)*2
day = day % 7
  
nly =[31, 28, 31, 30, 31, 30
      31, 31, 30, 31, 30, 31]
ly =[31, 29, 31, 30, 31, 30
     31, 31, 30, 31, 30, 31]
s = 0
  
if yy % 4 == 0:
    for i in range(mm-1):
        s+= ly[i]
else:
    for i in range(mm-1):
        s+= nly[i]
  
day += s % 7
day = day % 7
   
# variable used for white space filling 
# where date not present
space =''
space = space.rjust(2, ' ')
  
# code below is to print the calendar
print(month[mm], yy)
print('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa')
  
if mm == 9 or mm == 4 or mm == 6 or mm == 11
    for i in range(31 + day):
          
        if i<= day:
            print(space, end =' ')
        else:
            print("{:02d}".format(i-day), end =' ')
            if (i + 1)% 7 == 0:
                print()
elif mm == 2:
    if yy % 4 == 0:
        p = 30
    else:
        p = 29
          
    for i in range(p + day):
        if i<= day:
            print(space, end =' ')
        else:
            print("{:02d}".format(i-day), end =' ')
            if (i + 1)% 7 == 0:
                print() 
else:
    for i in range(32 + day):
          
        if i<= day:
            print(space, end =' ')
        else:
            print("{:02d}".format(i-day), end =' ')
            if (i + 1)% 7 == 0:
                print()


Output:

February 2020
Su Mo Tu We Th Fr Sa
                  01 
02 03 04 05 06 07 08 
09 10 11 12 13 14 15 
16 17 18 19 20 21 22 
23 24 25 26 27 28 29


Similar Reads

Python DateTime - DateTime Class
DateTime class of the DateTime module as the name suggests contains information on both dates as well as time. Like a date object, DateTime assumes the current Gregorian calendar extended in both directions; like a time object, DateTime assumes there are exactly 3600*24 seconds in every day. But unlike the date class, the objects of the DateTime cl
5 min read
How to convert a Python datetime.datetime to excel serial date number
This article will discuss the conversion of a python datetime.datetime to an excel serial date number. The Excel "serial date" format is actually the number of days since 1900-01-00. The strftime() function is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns the string r
3 min read
How to Fix - "datetime.datetime not JSON serializable" in Python?
In this article, we are going to learn how to fix the error "datetime.datetime not JSON serializable" in Python. datetime.datetime is a class in the Python datetime module that represents a single point in time. This class is not natively supported by the JSON (JavaScript Object Notation) format, which means that we cannot serialize a datetime.date
4 min read
Manipulate Date and Time with the Datetime Module in Python
Have you ever wondered about working with Date and Time with Python? If you have then you must have noticed that Python does not provide any built-in method to work with either Date or Time. But thanks to the DateTime module that comes pre-loaded with Python's standard utility modules we can easily manipulate date and time according to our own need
9 min read
Python datetime module
In Python, date and time are not data types of their own, but a module named DateTime in Python can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no need to install it externally. In this article, we will explore How DateTime in Python works and what are the main classes of DateTime m
14 min read
Calendar Functions in Python | Set 1( calendar(), month(), isleap()...)
Python defines an inbuilt module "calendar" which handles operations related to the calendar.Operations on the calendar : 1. calendar(year, w, l, c):- This function displays the year, the width of characters, no. of lines per week, and column separations.2. firstweekday() :- This function returns the first week day number. By default 0 (Monday). C/
5 min read
Python calendar module : formatmonth() method
Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. class calendar.TextCalendar(firstweekday=0) can be used to generate plain text
2 min read
Python calendar module | itermonthdays2() method
Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. itermonthdays2() method is used to get an iterator for the month in the year s
2 min read
Python calendar module : iterweekdays() method
Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. iterweekdays() method returns an iterator for the week day numbers that will b
1 min read
Python calendar module : itermonthdays() method
Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. itermonthdays() method returns an iterator of a specified month and a year. Da
1 min read