Calendar Module
Calendar Module
In [3]:
import calendar
print(calendar.calendar(2024))
2024
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
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
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
In [6]:
import calendar
print(calendar.isleap(2023))
False
In [8]:
import calendar
print(calendar.leapdays(2000,2024))
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)
In [13]:
print(calendar.weekday(2024,9,20))
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>
In [21]:
import calendar
def find_second_saturdays(year):
second_saturdays = []
# Get the matrix of the month with the days laid out in weeks
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]
return second_saturdays
# Example usage:
year = 2024
second_saturdays = find_second_saturdays(year)
print(f"Second Saturdays in the year {year}:")
print(f"{year}-{month:02d}-{day:02d}")
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
In [26]:
import calendar
def count_bussiness_days(year):
bussiness_days=0
month_cal=calendar.monthcalendar(year,month)
if week[day]!=0:
bussiness_days+=1
return bussiness_days
In [27]:
import calendar
def count_holidays(year):
holidays = 0
# Get the matrix of the month with the days laid out in weeks
holidays += 1
holidays += 1
return holidays
# Example usage:
year = 2024
holidays = count_holidays(year)
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
In [36]:
import datetime
print(datetime.timedelta(days=5,hours=3))
5 days, 3:00:00
using strptime()
In [50]:
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]
In [58]:
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]
In [59]:
now=datetime.now()
timestamp=now.timestamp()
print(timestamp)
1726902414.330421
In [60]:
now_utc=datetime.now(timezone.utc)
print(now_utc)
2024-09-21 07:08:15.892688+00:00
In [61]:
d=date(2024,9,21)
t=time(12,30)
dt=datetime.combine(d,t)
print(dt)
2024-09-21 12:30:00
In [62]:
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
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)
return second_saturdays
# Example usage:
year = 2024
second_saturdays = find_second_saturdays(year)
def count_holidays(year):
holidays = 0
return holidays
# Example usage:
year = 2024
holidays = count_holidays(year)
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
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}")