In this article, we will learn how to find the day of the week for a given date in python.
There are many ways to find the day of a week for a given date. In this article, to find the day of the week for a given date, we use the strptime()
, strftime()
or weekday()
function.
Here are some examples to find the day of the week for a given date.
In this example, we will define the list with the names of the days of a week and weekday()
function returns an integer corresponding to the day of a week. And strptime(
) function used to format the input to a date format and these functions import from the datetime
module.
Example 1: By Using strptime() function
# Find the Day of the Week for a Given Date By Using strptime() function
# Import Module
import datetime
# Take the Input From the User
date=str(input('Enter the date(for e.g.:09-02-2020): '))
# Day Name List
day_name= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
day = datetime.datetime.strptime(date, '%d-%m-%Y').weekday()
# Print the Day
print("The Weekday for a Given date({0}) is {1}".format(date,day_name[day]))
Enter the date(for e.g.:09-02-2020): 15-02-2020
The Weekday for a Given date(15-02-2020) is Saturday
Enter the date(for e.g.:09-02-2020): 20-05-2020
The Weekday for a Given date(20-05-2020) is Wednesday
In this example, we will split the Date into the day, month, and year using the split()
function. And the strftime()
function takes the DateTime format specifier and returns a formatted string. Here we pass the “%A
” to get the gull weekday name for a given date and if you want abbreviations of a weekday name for a given date, use the directive “%a
”.
# Find the Day of the Week for a Given Date By Using strftime() function
# Import Module
import datetime
from datetime import date
# Take the Input From the User
date=str(input('Enter the date(for e.g.:09-02-2020): '))
# Split Date into day, year and month
day, month, year = date.split('-')
day_name = datetime.date(int(year), int(month), int(day))
# Print the Day
# Print Full Weekday Name
print("The Weekday for a Given date({0}) is {1}".format(date,day_name.strftime("%A")))
# Print Abbreviations of Weekday Name
print("The Weekday for a Given date({0}) is {1}".format(date,day_name.strftime("%a")))
Enter the date(for e.g.:09-02-2020): 15-02-2020
The Weekday for a Given date(15-02-2020) is Saturday
The Weekday for a Given date(15-02-2020) is Sat
Enter the date(for e.g.:09-02-2020): 20-05-2020
The Weekday for a Given date(20-05-2020) is Wednesday
The Weekday for a Given date(20-05-2020) is Wed
In this example, we will use the weekday()
function from datetime
module to get the weekday number and calendar.day_name[WeekdayNumber]
to get the full weekday name and calendar.day_abbr[WeekdayNumber]
to get the weekday name abbreviations where WeekdayNumber is the number of the day you want the name.
# Find the Day of the Week for a Given Date By Using weekday() function provided by datetime module
# Import Module
import datetime
import calendar
# Take the Input From the User
date=str(input('Enter the date(for e.g.:09-02-2020): '))
# Weekday Number
WeekdayNumber = datetime.datetime.strptime(date, '%d-%m-%Y').weekday()
# Print the Day
# Print Full Weekday Name
print("The Weekday for a Given date({0}) is {1}".format(date,calendar.day_name[WeekdayNumber]))
# Print Abbreviations of Weekday Name
print("The Weekday for a Given date({0}) is {1}".format(date,calendar.day_abbr[WeekdayNumber]))
Enter the date(for e.g.:09-02-2020): 20-05-2020
The Weekday for a Given date(20-05-2020) is Wednesday
The Weekday for a Given date(20-05-2020) is Wed
I hope this article will help you to understand how to get the day name of the week for a given date in python.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments