Get day of week in Python

Get day of week in Python

Date and time play an important role in python. We may have to deal with time-series data or columns which contain a timestamp. There are many modules that allow us to work with date and time values.

In this article, we will discuss how to get day of week in Python.

Using the datetime.weekday() function

Date is not a defined datatype in Python. However, we can work with date objects by using the datetime module. This module allows us to create and work with such objects. The weekday() function from this module is used to get day of week from datetime in Python.

For example,

Output:

6

In the above example datetime() constructor is used to initiating a date object. The weekday() function returns a value from 0 to 6. Here 0 is considered as Monday as the start of the week and 6 as the last day of the week that is Sunday.

We can add a small line of code to get the name of the day from this number.

Output:

Sunday

In the above code, we created a list of the name of days and every day was at its corresponding value based on the weekday() function. This is best way to get day of week in Python.

Using the datetime.isoweekday() function

This function works similar to the weekday() function mentioned in the previous method. However, it returns 1 for the first day of the week that is Monday, and 7 for the last day.

We can use the above list method to get the name of the day by just making a slight alteration to the code.

For example,

Output:

Sunday

Using the datetime.strftime() function

The strftime() function is used to return some string based on the format from a given date. We can use it get the weekday from a date as shown below.

Output:

Sunday

Note that in this method we can use the %a format to get the weekday name in short and the %w format to get the day number.

Using the calendar.weekday() function

The calendar module can be used for our problem as an alternative. This module is equipped with different functions for calendar related operations. The weekday() function from this module works similarly to the one in the datetime module.

For example,

Output:

6

We can use the day_name object from this module to get the name of the weekday. It works similar to the list method discussed earlier in the previous methods.

Output:

Sunday

Using the pandas.dayofweek() function

The pandas library deals with data containing dates very frequently in time-series data or Date columns. Therefore the module is well equipped to work with such values.

The dayofweek() function from this module returns the day of week of a given date. It also considers that the week starts with Monday and returns 0 for it and 6 for Sunday.

For example,

Output:

6

Note that we use the pandas.Timestamp() to return a datetime object. This function works only for datetime values and can provide the day of the week for DataFrames or Series objects at once. We can use the list method discussed in the earlier methods to get the name of the day also.

Using the pandas.weekday_name() function

The weekday_name() function returns the name of the day from a given date. It works for DataFrames and Series objects also.

For example,

Output:

Sunday

Note that this function is deprecated in the recent versions of the module so should be avoided.

Using dateutil.parser() function

This function is also used to format or parse dates. It can return the weekday using the strftime() function by passing the required format to the function.

For example,

Output:

Sunday

Note that the format of the given date can be altered as per the user and the result will be the same.

Using a user-defined function

If one has to avoid all the modules discussed above at all costs, then we can create our own little function which will return the number of the weekday from a given day.

This method assumes that the change to the Georgian Calendar occurred in the year 1582 and uses that value.

See the code below.

Output:

7

In the above method, we consider Sunday as the first day of the week and assign it as 1 and Saturday as the last day with value as 7.

That’s all about how to get day of week in Python.

Related Posts

  • Detect keypress in Python
    12 June

    Detect keypress in Python

    Table of ContentsUsing the keyboard module to detect keypress in PythonUsing the pynput module to detect keypress in PythonUsing the msvcrt module to detect keypress in Python Python allows us to work with user input in its programs. We can also work with hardware devices in Python. In this article, we will discuss how to […]

  • Copy a dictionary in Python
    12 June

    7 ways to copy a dictionary in python

    Table of ContentsUsing the copy() function to copy a dictionary in PythonUsing the deepcopy() function to copy a dictionary in PythonUsing the dict() function to copy a dictionary in PythonUsing the unpack operator ** to copy a dictionary in PythonUsing the update() function to copy a dictionary in PythonUsing the dictionary comprehension method to copy […]

  • Escape quotes in Python
    11 June

    Escape quotes in Python

    Table of ContentsUsing \ to escape quotes in PythonUsing \ with replace() to escape quotes in PythonUsing json.dumps to escape quotes in PythonUsing triple single or double quotes to escape quotes in pythonUse r to include \ and quotes in String 💡 Outline You can use \ to escape quotes in Python. If you want […]

  • Sleep for milliseconds in python
    31 May

    How to sleep for milliseconds(ms) in Python

    Table of ContentsUse time.sleep() function to make the program sleep for millisecondsUse threading.Timer() function to make the program sleep for milliseconds 💡 Outline You can use time.sleep() method to sleep for milliseconds in Python. If you want to delay python program by 500 ms, then you need to pass 0.5 as parameter to sleep method. […]

  • Convert unix timestamp to datetime in Python
    21 May

    Convert Unix timestamp to DateTime in python

    Table of ContentsUsing fromtimestamp() FunctionUsing strftime() FunctionUsing to_datetime() Function A UNIX timestamp is defined as the several numbers of seconds between a particular date and the Epoch i.e January 1, 1970 at UTC. In this tutorial, we will see some methods by which we can convert a UNIX timestamp to DateTime in Python. Using fromtimestamp() […]

  • Python round to 2 decimal places
    21 May

    How to round to 2 decimals in Python

    Table of ContentsUsing the round() functionUsing the format() functionUsing the % formattingUsing the f-stringsUsing the math.ceil() and math.floor() functions In Python, we deal with integers, floating values, and even imaginary numbers. Float numbers are used to represent the real numbers with integers and decimal parts. Python represents such numbers with 64-bit precision giving it a […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.