find day of week in python

In this python example, you will find week day of date using different methods

Using the datetime Library

For Date and time operation python has in-built library called datetime.This library provides the method .weekday(), which returns a number between 0 and 7, representing the week’s day. So we just have to map return of this function.

Example - 1: using datetime library

    
import datetime

week_days=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
day = datetime.date(2020,12,25).weekday()
print(week_days[day])
   

Output :

    
Friday
    

In Above example, weekday() function will return integer number and we will just print that index from our list.

Using calendar library

The Calendar library is another built-in python library handy when it comes to date manipulation. It also has weekday method which will take date as parameter and returns interger value of day. This value can be mapped with our week day name list like previous example.

Example - 2: using calendar library

    
import calendar

week_days=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
day= calendar.weekday(2020,12,24)
print(week_days[day])
   

Output :

    
Thursday
    

Using datetime.strftime method

The datetime.strftime method also used for getting day of week. This function will convert date into sting so we can print it in any format we want.

Example - 3: using datetime.strftime function

    
import datetime

print(datetime.date(2020,12,25).strftime('%A'))
   

Output :

    
Friday
    

These are common method which we can use to find day of week as per our requirement.


Share your thoughts

Ask anything about this examples