# Date and time in # Python
an introduction
Python contains more than one modwell ready to handle the date, time and calendar with ease.
In this lesson, you will learn how to take advantage of the module datetimeand module calendarto display the date, time and calendar as you need.
an introduction Python contains more than one Moidwell ready to handle date, time and calendar with ease. In this lesson, you will learn how to take advantage of the datetime and calendar modules to display the date, time and calendar as you need.
Module datetimein Python
The module datetimecontains a set of classes with functions ready to deal with the date and time.
date: Contains a set of special functions to deal with the date.time: Contains a set of special functions to deal with time.datetime: Contains a set of special functions to deal with the date and time.timedelta: Contains a set of special functions to accurately calculate the difference between one date and another.timezone: Contains a set of special functions to calculate the time difference between one date and another according to the time zone of each date.
In the following exercise, we store the current date and time depending on the function now()in the class datetimeinside the moduledatetime.
In the following example, we have stored the current date and time based on the now () function in the datetime class inside the datetime.n module.
First exercise
# datetime Here we have included all the content of the module import datetime # dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return dt = datetime.datetime.now() # dt Here we have shown the value of the object print(dt)
• We will get a result similar to the following when running.
We notice from the previous example that the class datetimeallows us to get the date and time with high accuracy because it gave us the following information in order:
year, month, day, hour, minute, second, next fractions of a second.
Of course, the class datetimecontains ready-made functions and features that allow you to deal with the date and time however you want, to store it or display it as you want.
Do not worry, we will learn about the characteristics and functions of the class datetimein detail in this lesson.
Class Constructor datetimein Python
The class datetimecontains the following constructor.
class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
So, when creating an objectdatetime,You can directly enter a date and time in it.
Actually, you are forced to enter a value in place of the parameters and yearbecause they are not given default values.
For other parameters, you can set their default values or not because they are given default values.monthday
The values you can pass for the parameters are the following:
year: an integer whose value is within the range1 <= year <= 9999.month: an integer whose value is within the range1 <= year <= 12.day: An integer whose value is between the1last day of the month.hour: an integer whose value is within the range0 <= year < 24.minute: an integer whose value is within the range0 <= year < 60.second: an integer whose value is within the range0 <= year < 60.microsecond: an integer whose value is within the range0 <= year < 1000000.fold: an integer whose value is0or1.
If you pass any parameter value outside the allowed range, an error will occurValueError .
In the following exercise, we create a class object datetimewith the date we want to store it directly when creating it.
First exercise
# datetime Here we have included all the content of the module import datetime # dt represents a specific date and we stored it in the datetime object here we created an object from the class dt = datetime.datetime(2012, 4, 5) # dt Here we have shown the value of the object print(dt)
• We will get a result similar to the following when running.
In the following exercise, we create an object from the class datetimewith the date and time we want to store it directly when creating it.
second exercise
# datetime Here we have included all the content of the module import datetime # dt represents a specific date + time and we stored it in the datetime object here we created an object from the class dt = datetime.datetime(2012, 4, 5, 7, 30, 46) # dt Here we have shown the value of the object print(dt)
• We will get a result similar to the following when running.
Class properties datetimein Python
The following table contains the characteristics of the class datetimein the moduledatetime.
| fixed name | tariff |
|---|---|
year |
Contains the year number stored in an objectThe datetime. |
month |
Contains the month number stored in an objectThe datetime. |
day |
Contains the day number stored in an objectThe datetime. |
hour |
Contains the clock number stored in an objectThe datetime. |
minute |
Contains the minute number stored in an objectThe datetime. |
second |
Contains the number of seconds stored in an objectThe datetime. |
microsecond |
Contains the number of parts of seconds stored in an objectThe datetime. |
In the following exercise we have shown only the year of the date stored in an objectthedatetimeDepending on the featureyear.
exercise
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# dt in the year object Here we have shown the value of the property
print('This tutorial is written in', dt.year)
• We will get a result similar to the following when running.
Formatting the date and time in Python
Initially, what is meant by defining formate(Format)The date and time is displayed, stored, or differently.
To specify the date and time formats we want to show, we use a ready-made function in the class datetimecalledstrftime().
Using this function is very easy, as we call it from an objectthedatetimeThen we pass it a separate symbol or set of symbols to specify the things we want to return to us as text.
In the following example, we relied on the function strftime()in the class datetimeto display the name of the current month.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# and then we print dt for it as text to return the month name stored in object %B and pass the code strftime() here we called the function
print(dt.strftime('%B'))
• We will get a result similar to the following when running.
Python symbols that can be used in a functionstrftime()
The following table contains the symbols that you can use when defining the date and time formats using the functionstrftime().
| code | use it | Example |
|---|---|---|
%a |
To display the name of the day in short | Mon |
%A |
To display the full name of the day | Monday |
%w |
Shows the day number for the week. Here is the first day considered Sunday(Sunday)And it is equal to0 ,And the last day of the week is Saturday(Saturday)And it is equal to 6 |
1 |
%d |
Shows the number of the day relative to the month, considering that this number will be within the range01 - 31 |
03 |
%b |
To display the name of the month in short | Dec |
%B |
To display the full month name | December |
%m |
Returns the number of the month in relation to the year, considering that this number will be within the range01 - 12 |
12 |
%y |
To show the year number in short, that is, to show only the first two digits of it. | 18 |
%Y |
to show the year number. | 2018 |
%H |
Shows the hour number in a 24 - hour clock, taking into account that this number will be within the range00 - 23 |
14 |
%I |
Shows the clock number in the 12 - hour clock, taking into account that this number will be within the range00 - 11 |
2 |
%p |
To show the word AMif the time is before 12 noon and to show the word PMif the time is after |
PM |
%M |
To show the minute number, given that this number will be within the range00 - 59 |
24 |
%S |
Shows the number of the second, considering that this number will be within the range00 - 59 |
09 |
%f |
to show fractions of a secondwithMicrosecond,That is, to show one second as a million parts. Taking into account that this number will be within the range000000 - 999999 |
034208 |
%j |
Returns the number of the day relative to the year, considering that this number will be within the range001 - 366 |
337 |
%U |
Shows the number of the week relative to the year, taking into account that this number will be within the range 00 - 53and the first day of each week is Sunday(Sunday) |
48 |
%W |
Shows the week number for the year, taking into account that this number will be within the range 00 - 53and the first day of each week is Monday(Monday) |
49 |
%c |
To display the full date and time | Mon Dec 3 18:52:05 2018 |
%x |
To show the current date in full | 12/03/18 |
%X |
To show the full current time | 18:52:05 |
Extensive examples of each symbol in the table
Python datetime.strftime('%a')
In the following example we will pass the symbol %aas text to the function strftime()to show the name of the day in short.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# in short and then we print dt for it as text to return the name of the day stored in object %a and pass the code strftime() here we called the function
print(dt.strftime('%a'))
• We will get a result similar to the following when running.
Python datetime.strftime('%A')
In the following example we will pass the symbol %Aas text to the function strftime()to show the name of the day.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# and then we print it dt as text to return the name of the day stored in object %A and pass the code strftime() here we called the function
print(dt.strftime('%A'))
• We will get a result similar to the following when running.
Python datetime.strftime('%w')
In the following example we will pass the symbol %was text to the function strftime()to show the number of the day in relation to the week.
Note: Here is the first day considered Sunday(Sunday)And it is equal to0 ,And the last day of the week is Saturday(Saturday)And it is equal to 6
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# and then we print it dt today stored in its index object as text so it returns the number %w and pass the code strftime() here we called the function
print(dt.strftime('%w'))
• We will get a result similar to the following when running.
Python datetime.strftime('%d')
In the following example we will pass the symbol %das text to the function strftime()to show the number of the day in relation to the month, given that this number will be within the range01 - 31
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# relative to the month and then we print dt for it as text to return the number of the day stored in object %d and pass the code strftime() here we called the function
print(dt.strftime('%d'))
• We will get a result similar to the following when running.
Python datetime.strftime('%b')
In the following example we will pass the symbol %bas text to the function strftime()to show the name of the month in a nutshell.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# in short and then we print dt for it as text to return the name of the month stored in object %b and pass the code strftime() here we called the function
print(dt.strftime('%b'))
• We will get a result similar to the following when running.
Python datetime.strftime('%B')
In the following example we will pass the symbol %Bas text to the function strftime()to show the name of the month.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# and then we print dt for it as text to return the month name stored in object %B and pass the code strftime() here we called the function
print(dt.strftime('%B'))
• We will get a result similar to the following when running.
Python datetime.strftime('%m')
In the following example we will pass the symbol %mas text to the function strftime()to show the month number in relation to the year, given that this number will be within the range01 - 12
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# relative to the year and then we print dt for it as text to return the month number stored in object %m and pass the code strftime() here we called the function
print(dt.strftime('%m'))
• We will get a result similar to the following when running.
Python datetime.strftime('%y')
In the following example, we will pass the symbol %yas text to the function strftime()to show the year number briefly, that is, to show only the first two digits of it.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# in short and then we print dt for it as text to return the year number stored in object %y and pass the code strftime() here we called the function
print(dt.strftime('%y'))
• We will get a result similar to the following when running.
Python datetime.strftime('%Y')
In the following example we will pass the symbol %Yas text to the function strftime()to show the year number.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# and then we print dt for it as text to return the year number stored in object %Y and pass the code strftime() here we called the function
print(dt.strftime('%Y'))
• We will get a result similar to the following when running.
Python datetime.strftime('%H')
In the following example we will pass the symbol %Has text to the function strftime()to show the 24 - hour clock number, given that this number will be within the range00 - 23
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# and then we print dt for it as text to return the clock number stored in object %H and pass the code strftime() here we called the function
print(dt.strftime('%H'))
• We will get a result similar to the following when running.
Python datetime.strftime('%I')
In the following example, we will pass the symbol %Ias text to the function strftime()to show the 12 - hour clock number, given that this number will be within the range00 - 11
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# and then we print dt for it as text to return the clock number stored in object %I and pass the code strftime() here we called the function
print(dt.strftime('%I'))
• We will get a result similar to the following when running.
Python datetime.strftime('%p')
In the following example we will pass the symbol %pas text to the function strftime()to show the word AMif the time is before 12 noon and to show the word PMif the time is after.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# and then we print it dt as text to return the clock number stored in object %p and pass the code strftime() here we called the function
print(dt.strftime('%p'))
• We will get a result similar to the following when running.
Python datetime.strftime('%M')
In the following example we will pass the symbol %Mas text to the function strftime()to show the number of the minute, taking into account that this number will be within the range00 - 59.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# and then we print it dt as text to return the minute number stored in object %M and pass the code strftime() here we called the function
print(dt.strftime('%M'))
• We will get a result similar to the following when running.
Python datetime.strftime('%S')
In the following example we will pass the symbol %Sas text to the function strftime()to show the number of the second, taking into account that this number will be within the range00 - 59.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# and then we print it dt it as text to return the number of seconds stored in object %S and pass the code strftime() here we called the function
print(dt.strftime('%S'))
• We will get a result similar to the following when running.
Python datetime.strftime('%f')
In the following example we will pass the symbol %fas text to the function strftime()to show fractions of a secondwithMicrosecond,That is, to show one second as a million parts. Taking into account that this number will be within the range000000 - 999999.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# and then we print dt for it as text to return the number of milliseconds stored in object %f and pass the code strftime() here we called the function
print(dt.strftime('%f'))
• We will get a result similar to the following when running.
Python datetime.strftime('%j')
In the following example we will pass the symbol %jas text to the function strftime()to show the number of the day in relation to the year, taking into account that this number will be within the range001 - 366
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# relative to the year and then we print dt for it as text to return the number of the day stored in object %j and pass the code strftime() here we called the function
print(dt.strftime('%j'))
• We will get a result similar to the following when running.
Python datetime.strftime('%U')
In the following example we will pass the symbol %Uas text to the function strftime()to show the week number relative to the year, taking into account that this number will be within the range 00 - 53and the first day of each week is Monday(Monday).
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# relative to the year and then we print dt for it as text to return the week number stored in object %U and pass the code strftime() here we called the function
print(dt.strftime('%U'))
• We will get a result similar to the following when running.
Python datetime.strftime('%W')
In the following example we will pass the symbol %Was text to the function strftime()to show the week number relative to the year, taking into account that this number will be within the range 00 - 53and the first day of each week is Sunday(Sunday).
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# relative to the year and then we print dt for it as text to return the week number stored in object %W and pass the code strftime() here we called the function
print(dt.strftime('%W'))
• We will get a result similar to the following when running.
Python datetime.strftime('%c')
In the following example, we will pass the code %cas text to the function strftime()to fully show the current date and time.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# completely and then we print dt to it as text to return the current date and time stored in object %c and pass the code strftime() here we called the function
print(dt.strftime('%c'))
• We will get a result similar to the following when running.
Python datetime.strftime('%x')
In the following example we will pass the code %xas text to the function strftime()to fully show the current date.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the current date and time that the function will return
dt = datetime.datetime.now()
# completely and then we print dt to it as text to return the current date stored in object %x and pass the code strftime() here we called the function
print(dt.strftime('%x'))
• We will get a result similar to the following when running.
Python datetime.strftime('%X')
In the following example we will pass the code %Xas text to the function strftime()to fully show the current time.
Example
# datetime Here we have included all the content of the module
import datetime
# dt in the datetime object as an object of the now() class here we have stored the time and the current time that the function will return
dt = datetime.datetime.now()
# completely and then we print dt to it as text to return the current time stored in object %X and pass the code strftime() here we called the function
print(dt.strftime('%X'))
• We will get a result similar to the following when running.
Module calendarin Python
The module calendarcontains a set of classes and functions ready to deal with the calendar and to display it very beautifully.
For example, it contains ready-made functions to display the calendar of a specific year or a specific month of the year.
In the following example, we display the calendar for the year 2018 based on the function prcal()in the class calendarinside the modulecalendar.
first example
# calendar Here we have included all the content of the module import calendar # To display the 2018 calendar from the prcal() class here we called the function calendar.prcal(2018)
• We will get the following result when running.
2018 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 4 8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11 15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18 22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25 29 30 31 26 27 28 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 1 2 3 4 5 6 1 2 3 2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17 16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24 23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30 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 1 2 3 4 5 1 2 2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9 9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 16 16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 23 23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 30 30 31 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 7 1 2 3 4 1 2 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 31
Python class functionscalendar
The following table contains the calendarmost commonly used class functions.
| Function name and definition | |
|---|---|
calendar.prcal(year, w=0, l=0, c=6, m=3)
Used to print a calendar for a specific year. Parameter location yearWe pass the year whose calendar we want to print. |
|
calendar.isleap(year)
Used to find out whether a year is a leap year or not. The place of the parameter yearwe pass the number of the year that we want to check. If a value yearrepresents a leap year, it is returnedTrue,If not, come backFalse. |
|
calendar.leapdays(y1, y2)
Returns the number of leap years that exist from a given year to a given year. Parameter location y1We pass the number of the year we want to start from. Parameter location y2We pass the number of the year before which we want to stop. |
|
calendar.setfirstweekday(weekday)
By default, when viewing the calendar, it appears on Monday(Monday)Like the first day of the week. This function is used to determine the first day each week will start. In place of the parameter weekdaywe pass one of the class constants calendarthat indicates the name of the day orIndexConstant:
| |
Python functionprcal()
its definition
Used to print a calendar for a specific year.
built
prcal.calendar(year, w=0, l=0, c=6, m=3)
parameters
The parameter
yearwe pass has a number representing the year whose calendar we want to print.Parameter
wYou can change its default value to specify the amount of space between every two calendar days.The parameter
lYou can change its default value to specify the number of blank lines under each month in the calendar.The parameter
cYou can change its default value to specify the amount of space between every two calendar months.The parameter
mYou can change its default value to specify a calendar every month will be displayed at the same level because by default a calendar every three months is displayed next to each other.
Return value
It does not return a value.
first example
# calendar Here we have included all the content of the module import calendar # To display the 2018 calendar from the prcal() class here we called the function calendar.prcal(2018)
• We will get the following result when running.
2018 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 4 8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11 15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18 22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25 29 30 31 26 27 28 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 1 2 3 4 5 6 1 2 3 2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17 16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24 23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30 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 1 2 3 4 5 1 2 2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9 9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 16 16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 23 23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 30 30 31 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 7 1 2 3 4 1 2 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 31
second example
# calendar Here we have included all the content of the module import calendar # To display the calendar year 2018. Specifying that we want to display every 4 months along with some calendar from the prcal() class here we called the function calendar.prcal(2018, m=4)
• We will get the following result when running.
2018 January February March April Mo Tu We Th Fr Sa Su 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 4 1 8 9 10 11 12 13 14 5 6 7 8 9 10 11 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 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 19 20 21 22 23 24 25 16 17 18 19 20 21 22 29 30 31 26 27 28 26 27 28 29 30 31 23 24 25 26 27 28 29 30 May June July August Mo Tu We Th Fr Sa Su 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 1 2 3 4 5 7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12 14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19 21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26 28 29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30 31 30 31 September October November December Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 24 25 26 27 28 29 30 29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 31
Python functionisleap()
its definition
Used to find out whether a year is a leap year or not.
built
calendar.isleap(year)
parameters
yearWe pass in its place a number representing the year we want to know if it is a leap or not.
Return value
Returns Trueif a value yearrepresents a leap year.
Otherwise, you will returnFalse.
Example
# calendar Here we have included all the content of the module import calendar # To find out whether the years 2018, 2019 and 2020 are leap years or not, and then we show what you will return every time calendar from the class isleap() here we called the function print(calendar.isleap(2018)) print(calendar.isleap(2019)) print(calendar.isleap(2020))
• We will get the following result when running.
False
True
Python functionleapdays()
its definition
Returns the number of leap years that exist from a given year to a given year.
built
calendar.leapdays(y1, y2)
parameters
Parameter location
y1We pass the number of the year we want to start from.Parameter location
y2We pass the number of the year before which we want to stop.
Return value
Returns the number of leap years that exist from a given year to a given year.
Example
# calendar Here we have included all the content of the module import calendar # To find out how many leap years there are from 2000 to before 2018. Then we show the number that leapdays() will return here we called the function print(calendar.leapdays(2000, 2018))
• We will get the following result when running.
Python functionsetfirstweekday()
its definition
By default, when viewing the calendar, it appears on Monday(Monday)Like the first day of the week.
This function is used to determine the first day each week will start.
built
calendar.setfirstweekday(weekday)
parameters
In place of the parameter weekdaywe pass one of the class constants calendarthat indicates the name of the day orIndexConstant:
calendar.MONDAYor 0 to set Monday as the first day of the week.calendar.TUESDAYor 1 to set Tuesday as the first day of the week.calendar.WEDNESDAYor 2 to set Wednesday as the first day of the week.calendar.THURSDAYor 3 to set Thursday as the first day of the week.calendar.FRIDAYor 4 to set Friday as the first day of the week.calendar.SATURDAYor 5 to designate Saturday as the first day of the week.calendar.SUNDAYor 6 to set Sunday as the first day of the week.
Return value
It does not return a value.
Possible errors
The calendar.IllegalWeekdayError is thrown if you pass a number out of range0 <= weekday &tl;= 6.
Example
# calendar Here we have included all the content of the module import calendar # To set Sunday as the first day of the weekday calendar.SUNDAY and pass the constant setfirstweekday() here we called the function calendar.setfirstweekday(calendar.SUNDAY) # To display the 2018 calendar from the prcal() class here we called the function calendar.prcal(2018)
• We will get the following result when running.
2018 January February March Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 1 2 3 1 2 3 7 8 9 10 11 12 13 4 5 6 7 8 9 10 4 5 6 7 8 9 10 14 15 16 17 18 19 20 11 12 13 14 15 16 17 11 12 13 14 15 16 17 21 22 23 24 25 26 27 18 19 20 21 22 23 24 18 19 20 21 22 23 24 28 29 30 31 25 26 27 28 25 26 27 28 29 30 31 April May June Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 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 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 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 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 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