0% found this document useful (0 votes)
3 views15 pages

Calendar Module

The document provides an overview of the Python calendar module, demonstrating various functionalities such as printing a yearly calendar for 2024, checking for leap years, and calculating business days and holidays. It also includes examples of using the datetime module to manipulate dates and times, parse date strings, and combine date and time objects. Additionally, it showcases how to find second Saturdays in a year and format calendars in HTML.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

Calendar Module

The document provides an overview of the Python calendar module, demonstrating various functionalities such as printing a yearly calendar for 2024, checking for leap years, and calculating business days and holidays. It also includes examples of using the datetime module to manipulate dates and times, parse date strings, and combine date and time objects. Additionally, it showcases how to find second Saturdays in a year and format calendars in HTML.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

calendar module

In [3]:

import calendar

print(calendar.calendar(2024))

2024

January February March

Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su

1 2 3 4 5 6 7 1 2 3 4 1 2 3

8 9 10 11 12 13 14 5 6 7 8 9 10 11 4 5 6 7 8 9 10

15 16 17 18 19 20 21 12 13 14 15 16 17 18 11 12 13 14 15 16 17

22 23 24 25 26 27 28 19 20 21 22 23 24 25 18 19 20 21 22 23 24

29 30 31 26 27 28 29 25 26 27 28 29 30 31

April May June

Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su

1 2 3 4 5 6 7 1 2 3 4 5 1 2

8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9

15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16

22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23

29 30 27 28 29 30 31 24 25 26 27 28 29 30

July August September

Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su

1 2 3 4 5 6 7 1 2 3 4 1

8 9 10 11 12 13 14 5 6 7 8 9 10 11 2 3 4 5 6 7 8

15 16 17 18 19 20 21 12 13 14 15 16 17 18 9 10 11 12 13 14 15

22 23 24 25 26 27 28 19 20 21 22 23 24 25 16 17 18 19 20 21 22

29 30 31 26 27 28 29 30 31 23 24 25 26 27 28 29

30
October November December

Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su

1 2 3 4 5 6 1 2 3 1

7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8

14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15

21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22

28 29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29

30 31

check the leap year or not

In [6]:

import calendar

print(calendar.isleap(2023))

False

leap years from the given range of years

In [8]:

import calendar

print(calendar.leapdays(2000,2024))

specified month printing

In [10]:

import calendar

print(calendar.month(2024,10))

October 2024

Mo Tu We Th Fr Sa Su

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
returning the tuple which contains the first day of the month and total numb er of days

In [12]:

import calendar

print(calendar.monthrange(2024,9))

(6, 30)

returns the weekdays for the specified date

In [13]:

import calendar ##0--6

print(calendar.weekday(2024,9,20))

returns the dates as a list

In [14]:

import calendar

print(calendar.monthcalendar(2024,9))

[[0, 0, 0, 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, 0, 0, 0, 0, 0, 0]]

In [16]:

import calendar

html_cal=calendar.HTMLCalendar(calendar.SUNDAY)

print(html_cal.formatmonth(2024,9))

<table border="0" cellpadding="0" cellspacing="0" class="month">

<tr><th colspan="7" class="month">September 2024</th></tr>

<tr><th class="sun">Sun</th><th class="mon">Mon</th><th class="tue">Tue</th><th


class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th
class="sat">Sat</th></tr>

<tr><td class="sun">1</td><td class="mon">2</td><td class="tue">3</td><td


class="wed">4</td><td class="thu">5</td><td class="fri">6</td><td class="sat">7</td></tr>

<tr><td class="sun">8</td><td class="mon">9</td><td class="tue">10</td><td


class="wed">11</td><td class="thu">12</td><td class="fri">13</td><td class="sat">14</td></tr>

<tr><td class="sun">15</td><td class="mon">16</td><td class="tue">17</td><td


class="wed">18</td><td class="thu">19</td><td class="fri">20</td><td class="sat">21</td></tr>

<tr><td class="sun">22</td><td class="mon">23</td><td class="tue">24</td><td


class="wed">25</td><td class="thu">26</td><td class="fri">27</td><td class="sat">28</td></tr>
<tr><td class="sun">29</td><td class="mon">30</td><td class="noday">&nbsp;</td><td
class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td
class="noday">&nbsp;</td></tr>

</table>

second saturdays in a ayear

In [21]:

import calendar

def find_second_saturdays(year):

second_saturdays = []

# Iterate over all the months in the year

for month in range(1, 13):

# Get the matrix of the month with the days laid out in weeks

month_cal = calendar.monthcalendar(year, month)

# Check if the first Saturday is in the first or second week

if month_cal[0][calendar.SATURDAY] != 0:

# If the first Saturday is in the first week, second Saturday is in the second week

second_saturday = month_cal[1][calendar.SATURDAY]

else:

# If the first Saturday is in the second week, second Saturday is in the third week

second_saturday = month_cal[2][calendar.SATURDAY]

# Add the second Saturday to the list

second_saturdays.append((year, month, second_saturday))

return second_saturdays

# Example usage:

year = 2024

second_saturdays = find_second_saturdays(year)
print(f"Second Saturdays in the year {year}:")

for (year, month, day) in second_saturdays:

print(f"{year}-{month:02d}-{day:02d}")

Second Saturdays in the year 2024:

2024-01-13

2024-02-10

2024-03-09

2024-04-13

2024-05-11

2024-06-08

2024-07-13

2024-08-10

2024-09-14

2024-10-12

2024-11-09

2024-12-14

calculate the bussiness days in a year

In [26]:

import calendar

def count_bussiness_days(year):

bussiness_days=0

for month in range(1,13):

month_cal=calendar.monthcalendar(year,month)

for week in month_cal:

for day in range(5): ##monday--friday(0--4)

if week[day]!=0:

bussiness_days+=1

return bussiness_days

year=int(input("enter the year"))


bussiness_days=count_bussiness_days(year)

print(f"total bussiness days in the year {year}:{bussiness_days}")

enter the year2023

total bussiness days in the year 2023:260

In [27]:

import calendar

def count_holidays(year):

holidays = 0

# Loop through each month in the year

for month in range(1, 13):

# Get the matrix of the month with the days laid out in weeks

month_cal = calendar.monthcalendar(year, month)

# Loop through each week in the month

for week in month_cal:

# Check Saturday (6) and Sunday (7)

if week[calendar.SATURDAY] != 0: # If the Saturday is a valid day of the month

holidays += 1

if week[calendar.SUNDAY] != 0: # If the Sunday is a valid day of the month

holidays += 1

return holidays

# Example usage:

year = 2024

holidays = count_holidays(year)

print(f"Total holidays (weekends) in the year {year}: {holidays}")


Total holidays (weekends) in the year 2024: 104

datetime module in python

In [31]:

import datetime

print(datetime.datetime.now())

2024-09-21 11:57:25.632994

date only

In [32]:

import datetime

print(datetime.date.today())

2024-09-21

time only

In [35]:

import datetime

t=datetime.time(14,30,45)

print(t)

14:30:45

timedelta--the difrence between two dates

In [36]:

import datetime

print(datetime.timedelta(days=5,hours=3))

5 days, 3:00:00

using strptime()

In [50]:

from datetime import datetime

import calendar

date_str="21-09-2024"

date_formats="%d-%m-%Y"
parsed_date=datetime.strptime(date_str,date_formats)

day_of_week=parsed_date.weekday()

weekname=calendar.day_name[day_of_week]

print(f"the date{date_str} falls on a {weekname}")

the date21-09-2024 falls on a Saturday

In [58]:

from datetime import datetime

import calendar

date_str="21-09-2024 12:34"

date_format="%d-%m-%Y %H:%M"

parsed_date=datetime.strptime(date_str,date_format)

day_of_week=parsed_date.weekday()

weekday_name=calendar.day_name[day_of_week]

print(f"the date and time {date_str} falls on a {weekday_name}")

the date and time 21-09-2024 12:34 falls on a Saturday

In [59]:

from datetime import datetime

now=datetime.now()

timestamp=now.timestamp()

print(timestamp)

1726902414.330421

In [60]:

from datetime import datetime,timezone

now_utc=datetime.now(timezone.utc)

print(now_utc)

2024-09-21 07:08:15.892688+00:00

combine() combining the diffrent parts of the date

In [61]:

from datetime import date,time,datetime

d=date(2024,9,21)
t=time(12,30)

dt=datetime.combine(d,t)

print(dt)

2024-09-21 12:30:00

In [62]:

from datetime import datetime

time_stamp=1726902414.330421

dt=datetime.utcfromtimestamp(time_stamp)

print(dt)

2024-09-21 07:06:54.330421

calendar module
In [3]:
import calendar
print(calendar.calendar(2024))
2024

January February March


Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 1 2 3
8 9 10 11 12 13 14 5 6 7 8 9 10 11 4 5 6 7 8 9 10
15 16 17 18 19 20 21 12 13 14 15 16 17 18 11 12 13 14 15 16 17
22 23 24 25 26 27 28 19 20 21 22 23 24 25 18 19 20 21 22 23 24
29 30 31 26 27 28 29 25 26 27 28 29 30 31

April May June


Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 5 1 2
8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9
15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16
22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23
29 30 27 28 29 30 31 24 25 26 27 28 29 30

July August September


Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 1
8 9 10 11 12 13 14 5 6 7 8 9 10 11 2 3 4 5 6 7 8
15 16 17 18 19 20 21 12 13 14 15 16 17 18 9 10 11 12 13 14 15
22 23 24 25 26 27 28 19 20 21 22 23 24 25 16 17 18 19 20 21 22
29 30 31 26 27 28 29 30 31 23 24 25 26 27 28 29
30
October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 1 2 3 1
7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8
14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15
21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22
28 29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29
30 31

check the leap year or not


In [6]:
import calendar
print(calendar.isleap(2023))
False

leap years from the given range of years


In [8]:
import calendar
print(calendar.leapdays(2000,2024))
6

specified month printing


In [10]:
import calendar
print(calendar.month(2024,10))
October 2024
Mo Tu We Th Fr Sa Su
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

returning the tuple which contains the first day of


the month and total numb er of days
In [12]:
import calendar
print(calendar.monthrange(2024,9))
(6, 30)

returns the weekdays for the specified date


In [13]:
import calendar ##0--6
print(calendar.weekday(2024,9,20))
4
returns the dates as a list
In [14]:
import calendar
print(calendar.monthcalendar(2024,9))
[[0, 0, 0, 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, 0, 0, 0, 0
, 0, 0]]
In [16]:
import calendar
html_cal=calendar.HTMLCalendar(calendar.SUNDAY)
print(html_cal.formatmonth(2024,9))
<table border="0" cellpadding="0" cellspacing="0" class="month">
<tr><th colspan="7" class="month">September 2024</th></tr>
<tr><th class="sun">Sun</th><th class="mon">Mon</th><th class="tue">Tue</th
><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><t
h class="sat">Sat</th></tr>
<tr><td class="sun">1</td><td class="mon">2</td><td class="tue">3</td><td c
lass="wed">4</td><td class="thu">5</td><td class="fri">6</td><td class="sat
">7</td></tr>
<tr><td class="sun">8</td><td class="mon">9</td><td class="tue">10</td><td
class="wed">11</td><td class="thu">12</td><td class="fri">13</td><td class=
"sat">14</td></tr>
<tr><td class="sun">15</td><td class="mon">16</td><td class="tue">17</td><t
d class="wed">18</td><td class="thu">19</td><td class="fri">20</td><td clas
s="sat">21</td></tr>
<tr><td class="sun">22</td><td class="mon">23</td><td class="tue">24</td><t
d class="wed">25</td><td class="thu">26</td><td class="fri">27</td><td clas
s="sat">28</td></tr>
<tr><td class="sun">29</td><td class="mon">30</td><td class="noday">&nbsp;<
/td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="no
day">&nbsp;</td><td class="noday">&nbsp;</td></tr>
</table>

second saturdays in a ayear


In [21]:
import calendar

def find_second_saturdays(year):
second_saturdays = []
# Iterate over all the months in the year
for month in range(1, 13):
# Get the matrix of the month with the days laid out in weeks
month_cal = calendar.monthcalendar(year, month)

# Check if the first Saturday is in the first or second week


if month_cal[0][calendar.SATURDAY] != 0:
# If the first Saturday is in the first week, second Saturday is in the second week
second_saturday = month_cal[1][calendar.SATURDAY]
else:
# If the first Saturday is in the second week, second Saturday is in the third week
second_saturday = month_cal[2][calendar.SATURDAY]

# Add the second Saturday to the list


second_saturdays.append((year, month, second_saturday))

return second_saturdays

# Example usage:
year = 2024
second_saturdays = find_second_saturdays(year)

print(f"Second Saturdays in the year {year}:")


for (year, month, day) in second_saturdays:
print(f"{year}-{month:02d}-{day:02d}")
Second Saturdays in the year 2024:
2024-01-13
2024-02-10
2024-03-09
2024-04-13
2024-05-11
2024-06-08
2024-07-13
2024-08-10
2024-09-14
2024-10-12
2024-11-09
2024-12-14

calculate the bussiness days in a year


In [26]:
import calendar
def count_bussiness_days(year):
bussiness_days=0
for month in range(1,13):
month_cal=calendar.monthcalendar(year,month)
for week in month_cal:
for day in range(5): ##monday--friday(0--4)
if week[day]!=0:
bussiness_days+=1
return bussiness_days

year=int(input("enter the year"))


bussiness_days=count_bussiness_days(year)
print(f"total bussiness days in the year {year}:{bussiness_days}")

enter the year2023


total bussiness days in the year 2023:260
In [27]:
import calendar

def count_holidays(year):
holidays = 0

# Loop through each month in the year


for month in range(1, 13):
# Get the matrix of the month with the days laid out in weeks
month_cal = calendar.monthcalendar(year, month)

# Loop through each week in the month


for week in month_cal:
# Check Saturday (6) and Sunday (7)
if week[calendar.SATURDAY] != 0: # If the Saturday is a valid day of the month
holidays += 1
if week[calendar.SUNDAY] != 0: # If the Sunday is a valid day of the month
holidays += 1

return holidays

# Example usage:
year = 2024
holidays = count_holidays(year)

print(f"Total holidays (weekends) in the year {year}: {holidays}")


Total holidays (weekends) in the year 2024: 104

datetime module in python


In [31]:
import datetime
print(datetime.datetime.now())

2024-09-21 11:57:25.632994
date only
In [32]:
import datetime
print(datetime.date.today())
2024-09-21

time only
In [35]:
import datetime
t=datetime.time(14,30,45)
print(t)
14:30:45

timedelta--the difrence between two dates


In [36]:
import datetime
print(datetime.timedelta(days=5,hours=3))

5 days, 3:00:00
using strptime()

In [50]:
from datetime import datetime
import calendar
date_str="21-09-2024"
date_formats="%d-%m-%Y"
parsed_date=datetime.strptime(date_str,date_formats)
day_of_week=parsed_date.weekday()
weekname=calendar.day_name[day_of_week]
print(f"the date{date_str} falls on a {weekname}")

the date21-09-2024 falls on a Saturday


In [58]:
from datetime import datetime
import calendar
date_str="21-09-2024 12:34"
date_format="%d-%m-%Y %H:%M"
parsed_date=datetime.strptime(date_str,date_format)
day_of_week=parsed_date.weekday()
weekday_name=calendar.day_name[day_of_week]
print(f"the date and time {date_str} falls on a {weekday_name}")
the date and time 21-09-2024 12:34 falls on a Saturday
In [59]:
from datetime import datetime
now=datetime.now()
timestamp=now.timestamp()
print(timestamp)
1726902414.330421
In [60]:
from datetime import datetime,timezone
now_utc=datetime.now(timezone.utc)
print(now_utc)
2024-09-21 07:08:15.892688+00:00
combine() combining the diffrent parts of the date
In [61]:
from datetime import date,time,datetime
d=date(2024,9,21)
t=time(12,30)
dt=datetime.combine(d,t)
print(dt)
2024-09-21 12:30:00
In [62]:
from datetime import datetime
time_stamp=1726902414.330421
dt=datetime.utcfromtimestamp(time_stamp)
print(dt)
2024-09-21 07:06:54.330421

You might also like