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:
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.
Available types of Date and Time!!
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:
datetime.tzinfo:An abstract base class for time zone information objects. These are used by the
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 object represents a duration, the difference between two dates or times.
The supported functions are given in the table as follows:
Operation Result t1 = t2 – t3 Difference of t2 and t3. Afterwards t1 == t2 – t3 and t2 == t1 + t3 are true. (1) t1 = t2 + t3 Sum of t2 and t3. Afterwards t1–t2 == t3 and t1–t3 == t2 are true. (1) t1 = t2 // i The floor is computed and the remainder (if any) is thrown away. (3) -t1 equivalent to
timedelta(-t1.days, –t1.seconds, –t1.microseconds), and to t1* -1. (1)(4)
+t1 Returns 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
Now we are providing you with different approaches by using Using
weekday() provided by
Approach 1: Using
weekday() provided by
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))
Approach 2: Using
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))
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))
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
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
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.