Python is a multi-function programming language. On contrary to, other languages like HTML, CSS, JavaScript, etc., it can be utilized for other kinds of programming. Python is not limited to any particular domain or range it is a vast resource language that provides vast scopes to different fields. For example – Web Development, Hacking, Data analysis, Machine learning, Game development web scraping, and many more. And python provides many such libraries and one such library is date-time. You can use datetime library to find python day of the week.

Python day of week

The  datetime library supplies classes for different methods of manipulating date and time in both easy way and some complex ways. There are two kinds of date and time objects:

  •  Naive
  • Aware

An aware object has sufficient knowledge of applicable algorithmic and political time adjustments. Such as time zone and daylight saving time information, to locate itself relative to other aware objects. An aware object represents a specific moment in time that is not open to interpretation for the Python day of week.

A naive object does not contain enough information to unambiguously locate itself relative to other datetime objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time. In some other time zone is purely up to the program, just like it’s up to the program. Whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality.

Also Read: Difference between loc() and iloc() in Pandas DataFrame

Available types of Date and Time!!

class datetime.date: An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Attributes: yearmonth, and day.

class datetime.time:An idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds (there is no notion of “leap seconds” here). Attributes: hourminutesecondmicrosecond, and tzinfo

class datetime.timedelta:A duration expressing the difference between two datetime, or datetime instances to microsecond resolution.

class datetime.tzinfo:An abstract base class for time zone information objects. These are used by the datetime and time classes to provide a customizable notion of time adjustment (for example, to account for time zone and/or daylight saving time) for Python day of week.

 Timedelta Objects:

timedelta object represents a duration, the difference between two dates or times.

The supported functions are given in the table as follows:

OperationResult
t1 = t2 – t3Difference of t2 and t3. Afterwards t1 == t2 – t3 and t2 == t1 + t3 are true. (1)
t1 = t2 + t3Sum of t2 and t3. Afterwards t1t2 == t3 and t1t3 == t2 are true. (1)
t1 = t2 // iThe floor is computed and the remainder (if any) is thrown away. (3)
-t1equivalent to timedelta(-t1.days, –t1.seconds, –t1.microseconds), and to t1* -1. (1)(4)
+t1Returns a timedelta object with the same value. (2)
str(t)Returns a string in the form [D day[s], ][H]H:MM:SS[.UUUUUU], where D is negative for negative t. (5)

Now we are providing you with different approaches by using Using weekday() provided by datetime module.

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.

# 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()  helps you make code in one or more argument and then on that basis returns a formatted string. Now we will pass “%A” directive as this will provide weekday name for the given date.

# 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

We will find the day number by using calendar module in this approach. And then we can find the corresponding weekday.

# 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

Approach 4: Series.dt.dayofweek

The day of the week with Monday=0, Sunday=6.

Return the day of the week. It is assumed the week starts on Monday, which is denoted by 0 and ends on Sunday which is denoted by 6. This method is available on both Series with datetime values (using the dt accessor) or DatetimeIndex.

import pandas as pd
s = pd.date_range('2016-12-31', '2017-01-08', freq='D').to_series()
s.dt.dayofweek

Output:

2016-12-31    5
2017-01-01    6
2017-01-02    0
2017-01-03    1
2017-01-04    2
2017-01-05    3
2017-01-06    4
2017-01-07    5
2017-01-08    6
Freq: D, dtype: int64

Conclusion:

We have provided you with python of the week program. And some other date time library to use and experience it with your own self by solving some examples. Hope you understand the date time libraries and Happy reading. Leave your comment if you like it Thank You.

Categorized in:

Tagged in: