Friday, December 27, 2024
Google search engine
HomeLanguagesPython program to find day of the week for a given date

Python program to find day of the week for a given date

Write a Python program to find the day of the week for any particular date in the past or future. Let the input be in the format “dd mm yyyy”.
Examples: 
 

Input : 03 02 1997 
Output : Monday

Input : 31 01 2019
Output : Thursday

The already discussed approach to find the day of the week for a given date is the Naive approach. Now, let’s discuss the pythonic approaches. 
Approach #1 : Using weekday() provided by datetime module.
The weekday() function of date class in datetime module, returns an integer corresponding to the day of the week. 
 

Python3




# Python program to Find day of
# the week for a given date
import datetime
import calendar
 
def findDay(date):
    born = datetime.datetime.strptime(date, '%d %m %Y').weekday()
    return (calendar.day_name[born])
 
# Driver program
date = '03 02 2019'
print(findDay(date))


Output: 

Sunday

 

  
Approach #2 : Using strftime() method
The strftime() method takes one or more format codes as an argument and returns a formatted string based on it. Here we will pass the directive “%A” in the method which provides Full weekday name for the given date. 
 

Python3




# Python program to Find day of
# the week for a given date
import datetime
from datetime import date
import calendar
 
def findDay(date):
    day, month, year = (int(i) for i in date.split(' '))   
    born = datetime.date(year, month, day)
    return born.strftime("%A")
 
# Driver program
date = '03 02 2019'
print(findDay(date))


Output: 

Sunday

 

  
Approach #3 : By finding day number 
In this approach, we find the day number using calendar module and then find the corresponding week day. 
 

Python3




# Python program to Find day of
# the week for a given date
import calendar
 
def findDay(date):
    day, month, year = (int(i) for i in date.split(' '))   
    dayNumber = calendar.weekday(year, month, day)
    days =["Monday", "Tuesday", "Wednesday", "Thursday",
                         "Friday", "Saturday", "Sunday"]
    return (days[dayNumber])
 
# Driver program
date = '03 02 2019'
print(findDay(date))


Output: 

Sunday

 

Time Complexity: O(1)

Auxiliary Space: O(1)

Approach#4: Using Zeller’s congruence

Algorithm

1. Use the strptime() method of the datetime module to convert the given input string into a datetime object.
2. Extract the day, month, and year from the datetime object.
3. Use Zeller’s congruence formula to calculate the day of the week.
4. Map the result from step 3 to the corresponding day of the week.

Python3




from datetime import datetime
 
 
def day_of_week(date_str):
    date_obj = datetime.strptime(date_str, '%d %m %Y')
    day = date_obj.day
    month = date_obj.month
    year = date_obj.year
    if month < 3:
        month += 12
        year -= 1
    century = year // 100
    year_of_century = year % 100
    day_num = (day + ((13 * (month + 1)) // 5) + year_of_century +
               (year_of_century // 4) + (century // 4) - (2 * century)) % 7-1
    day_names = ['Sunday', 'Monday', 'Tuesday',
                 'Wednesday', 'Thursday', 'Friday', 'Saturday']
    return day_names[day_num]
 
 
date_str = '03 02 2019'
print(day_of_week(date_str))


Output

Sunday

Time Complexity: O(1) – constant time is required to convert the input string into a datetime object and calculate the day of the week.
Space Complexity: O(1) – constant space is used.

RELATED ARTICLES

Most Popular

Recent Comments