weekday() Function Of Datetime.date Class In Python
The weekday() of datetime.date class function is used to return the integer value corresponding to the specified day of the week.
Syntax: weekday()
Parameters: This function does not accept any parameter.
Return values: This function returns the integer mapping corresponding to the specified day of the week. Below table shows the integer values corresponding to the day of the week.
| Integer returned by weekday() function | Day of the week |
|---|---|
| 0 | Monday |
| 1 | Tuesday |
| 2 | Wednesday |
| 3 | Thursday |
| 4 | Friday |
| 5 | Saturday |
| 6 | Sunday |
Example 1: Getting the integer values corresponding to the specified days of the week.
Python3
# Python3 code for getting# integer value corresponding# to the specified day of the week# Importing datetime moduleimport datetime# Specifying some date and time valuesdateTimeInstance1 = datetime.datetime(2021, 8, 1, 00, 00, 00)dateTimeInstance2 = datetime.datetime(2021, 8, 2, 00, 00, 00)dateTimeInstance3 = datetime.datetime(2021, 8, 3, 00, 00, 00)# Calling the weekday() functions over the# above dateTimeInstancesdayOfTheWeek1 = dateTimeInstance1.weekday()dayOfTheWeek2 = dateTimeInstance2.weekday()dayOfTheWeek3 = dateTimeInstance3.weekday()# Getting the integer value corresponding# to the specified day of the weekprint(dayOfTheWeek1)print(dayOfTheWeek2)print(dayOfTheWeek3) |
Output:
6 0 1
Example 2: Getting the date and time along with the day’s name of the entire week.
Python3
# Python3 code for getting# integer value corresponding# to the specified day of the week.# Importing datetime moduleimport datetime# Mapping of the week dayweekDaysMapping = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")# Specifying a date and time valuedateTimeInstance = datetime.datetime(2021, 8, 2, 00, 00, 00)# Calling the weekday() function over the above# specified weekday and date time valuedayOfTheWeek = weekDaysMapping[dateTimeInstance.weekday()]# Printing the date and time along with the# corresponding week day nameprint("{} is for {}".format(dateTimeInstance, dayOfTheWeek))# Getting on next daynextDay = dateTimeInstance.replace(day=3)# Calling the weekday() function over the above# specified weekday and date time valuedayOfTheWeek = weekDaysMapping[nextDay.weekday()]# Printing the date and time along with the# corresponding week day nameprint("{} is for {}".format(nextDay, dayOfTheWeek))# Getting on next daynextDay = dateTimeInstance.replace(day=4)# Calling the weekday() function over the above# specified weekday and date time valuedayOfTheWeek = weekDaysMapping[nextDay.weekday()]# Printing the date and time along with the# corresponding week day nameprint("{} is for {}".format(nextDay, dayOfTheWeek))# Getting on next daynextDay = dateTimeInstance.replace(day=5)# Calling the weekday() function over the above# specified weekday and date time valuedayOfTheWeek = weekDaysMapping[nextDay.weekday()]# Printing the date and time along with the# corresponding week day nameprint("{} is for {}".format(nextDay, dayOfTheWeek))# Getting on next daynextDay = dateTimeInstance.replace(day=6)# Calling the weekday() function over the above# specified weekday and date time valuedayOfTheWeek = weekDaysMapping[nextDay.weekday()]# Printing the date and time along with the# corresponding week day nameprint("{} is for {}".format(nextDay, dayOfTheWeek))# Getting on next daynextDay = dateTimeInstance.replace(day=7)# Calling the weekday() function over the above# specified weekday and date time valuedayOfTheWeek = weekDaysMapping[nextDay.weekday()]# Printing the date and time along with the# corresponding week day nameprint("{} is for {}".format(nextDay, dayOfTheWeek))# Getting on next daynextDay = dateTimeInstance.replace(day=8)# Calling the weekday() function over the above# specified weekday and date time valuedayOfTheWeek = weekDaysMapping[nextDay.weekday()]# Printing the date and time along with the# corresponding week day nameprint("{} is for {}".format(nextDay, dayOfTheWeek)) |
Output:
2021-08-02 00:00:00 is for Monday 2021-08-03 00:00:00 is for Tuesday 2021-08-04 00:00:00 is for Wednesday 2021-08-05 00:00:00 is for Thursday 2021-08-06 00:00:00 is for Friday 2021-08-07 00:00:00 is for Saturday 2021-08-08 00:00:00 is for Sunday






Please Login to comment...