Change language

Calendar Functions in Python | Set 2 (monthrange (), prcal (), weekday () …)

| |

1. monthrange (year, month) : — This function returns two integers, first, the number of the day of the week at the beginning (0 as Monday), and second, the number of days in the month .

2. prcal (year, w, l, c) : — This function also prints the calendar of a specific year, but does not require a print operation.

# Python code to demonstrate how it works
# monthrange () and prcal ()

 
# import of the calendar module for calendar operations

import calendar

 
# using monthrange () to print the weekday and
# month number

print ( "The start week number and no. of days of month:" , end = "")

print (calendar.monthrange ( 2008 , 2 ))

 
# using prcal () to print the 1997 calendar

print ( " The calendar of 1997 is: " )

calendar.prcal ( 1997 , 2 , 1 , 6 )

Exit:

 The start week number and no. of days of month: (4, 29) The calendar of 1997 is: 1997 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 1 2 1 2 6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9 13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16 20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23 27 28 29 30 31 24 25 26 27 28 24 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 1 2 3 4 1 7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8 14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15 21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22 28 29 30 26 27 28 29 30 31 23 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 1 2 3 1 2 3 4 5 6 7 7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14 14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21 21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28 28 29 30 31 25 26 27 28 29 30 31 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 1 2 1 2 3 4 5 6 7 6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14 13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21 20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28 27 28 29 30 31 24 25 26 27 28 29 30 29 30 31 

3. prmonth (year, month, w, l) : — This function also prints the month of a specific year, but does not require a "print" operation.

4. setfirstweekday (num) : — This function sets the starting number of the day of the week.

# Python code to demonstrate how it works
# prmonth () and setfirstweekday ()

 
# import of the calendar module for calendar operations

import calendar

 
# using prmonth () to print the 1997 calendar

print ( "The 4th month of 1997 is:" )

calendar.prmonth ( 1997 , 4 , 2 , 1 )

 

 
# using setfirstweekday () to set the first week day number

calendar.setfirstweekday ( 4 )

 

print ( " " )

 
# using firstweekday () to check the changed day

print ( "The new week day number is:" , end = "")

print (calendar.firstweekday ())

Output:

 The 4th month of 1997 is: April 1997 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 The new week day number is: 4 

5. weekday (year, month, date) : — this function returns weekday number (0 — Monday) of the date specified in its arguments.

# Python code to demonstrate how it works
# weekday ()

 
# import of the calendar module for calendar operations

import calendar

 
# using the day of the week () to display the day number of the date

print ( "The day number of 25 April 1997 is: " , end = " ")

print (calend ar.weekday ( 1997 , 4 , 25 ))

Output:

 The day number of 25 April 1997 is: 4 

This article is courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically