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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

  • 11 June

    Save Numpy Array as Image in Python

    Table of ContentsSave numpy array as image in PythonHow to save numpy array as image in Python?Using the PIL library to save numpy array as image in PythonUsing the matplotlib.image library to save numpy array as image in PythonUsing the opencv library to save numpy array as image in PythonUsing the imageio library to save […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.