Get Day name from Weekday int

day of the week python code
golang get day of week
python days of week list
python get weekday
calendar weekday name
golang get first day of week
how to get day of the week from datetime
python weekday number

I have a weekday integer (0,1,2...) and I need to get the day name ('Monday', 'Tuesday',...).

Is there a built in Python function or way of doing this?

Here is the function I wrote, which works but I wanted something from the built in datetime lib.

def dayNameFromWeekday(weekday):
    if weekday == 0:
        return "Monday"
    if weekday == 1:
        return "Tuesday"
    if weekday == 2:
        return "Wednesday"
    if weekday == 3:
        return "Thursday"
    if weekday == 4:
        return "Friday"
    if weekday == 5:
        return "Saturday"
    if weekday == 6:
        return "Sunday"

It is more Pythonic to use the calendar module:

>>> import calendar
>>> list(calendar.day_name)
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

Or, you can use common day name abbreviations:

>>> list(calendar.day_abbr)
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

Then index as you wish:

>>> calendar.day_name[1]
'Tuesday'

(If Monday is not the first day of the week, use setfirstweekday to change it)

Using the calendar module has the advantage of being location aware:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
'de_DE'
>>> list(calendar.day_name)
['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag']

How to find the day of week · YourBasic Go, Weekday type specifies a day of the week (Sunday = 0, …). type Weekday int const ( Sunday Weekday = iota Monday Tuesday Wednesday Thursday Friday  The time.Weekday type specifies a day of the week (Sunday = 0, …). type Weekday int const ( Sunday Weekday = iota Monday Tuesday Wednesday Thursday Friday Saturday ) Share this page :

I have been using calendar module:

import calendar
calendar.day_name[0]
'Monday'

weekday function in Python, The python weekday function of class date returns the day of the week as an Function Name: weekday The weekday() function of date class in datetime module, returns an integer Find out what day of the week is this year's Christmas. By day name, I mean Monday or Tuesday for example, and not the date number or number of the day of the week (which you can also get if you need it). Here are three ways to return the day name from a date in SQL Server using T-SQL. The FORMAT() Function. The FORMAT() function returns a value formatted in the specified format and optional culture. You can use it to return the day name from a date. Here’s an example:

If you need just to print the day name from datetime object you can use like this:

current_date = datetime.now
current_date.strftime('%A')  # will return "Wednesday"
current_date.strftime('%a')  # will return "Wed"

check this converting week number (int) into week day name (string , def dayNameFromWeekday(weekday): if weekday>6: print('error'); return 'an unknown day' return days[weekday] ## get keyboard input but convert it to int nb​  The Weekday returns an integer representing the day of week, starting from 1=Sunday. WeekdayName functions starts the week as Monday=1. That's why it's out of sync and returning the wrong day name.

It's very easy:

week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
week[weekday]

DateAndTime.WeekdayName(Int32, Boolean, FirstDayOfWeek , Returns a string value containing the name of the specified weekday. public static string WeekdayName (int Weekday, bool Abbreviate = false, Microsoft. The following example uses the Weekday function to obtain the day of the week from  If you want to know the day of the week for your code to do something with it, DateTime.Now.DayOfWeek will do the job. If you want to display the day of week to the user, DateTime.Now.ToString("dddd") will give you the localized day name, according to the current culture (MSDN info on the "dddd" format string).

You could use a list which you get an item from based on your argument:

def dayNameFromWeekday(weekday):
    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    return days[weekday]

If you needed the function to not cause an error if you passed in an invalid number, for example "8", you could check if that item of the list exists before you return it:

def dayNameFromWeekday(weekday):
    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    return days[weekday] if 0 < weekday < len(days) else None

This function can be used like you'd expect:

>>> dayNameFromWeekday(6)
Sunday
>>> print(dayNameFromWeekday(7))
None

I'm not sure there's a way to do this built into datetime, but this is still a very efficient way.

How to: Extract the Day of the Week from a Specific Date, To extract the weekday name for the current culture, call the date and time value's DateTime. ToString(String) or DateTimeOffset. ToString(String) instance method, and pass the string "dddd" as the format parameter. The following example illustrates the call to the ToString(String) method. The getDay () method returns the day of the week (from 0 to 6) for the specified date. Note: Sunday is 0, Monday is 1, and so on. Browser Support. Date .getDay () Technical Details. A Number, from 0 to 6, representing the day of the week. JavaScript Version: Return the name of the weekday (not just a number): var d = new Date ();

DayName, DayName. Use this function to enter the name of the day of the week. Syntax. DayName (DayOfWeek, Locale) Enter an integer to designate the day of the week. 1 - Sunday This function is typically used with the WeekDay function. calendar.weekday(year, month, day) - Returns the day of the week (0 is Monday) for year (1970–…), month (1–12), day (1–31). calendar.day_name - An array that represents the days of the week in the current locale

Day of week - MATLAB weekday, DayNumber , DayName ] = weekday( D , DayForm ) returns the name for the day of the week in the format specified by DayForm , in US English. [ DayNumber  Define a list with the name of the days of a week named day_name. Then strptime() function formats the input string to a DD:MM:YYYY format. weekday() function returns an integer corresponding to the day of a week; Finally, print the day of the date by specifying that returned integer as an index value of a list day_name. Input:

VBA WEEKDAYNAME Function, The VBA WeekdayName Function - Returns the Weekday Name for a Supplied Integer An integer, between 1 and 7, representing the day of the week. Weekday_name is the inbuilt method for finding the day of the week in pandas python. Also dayofweek function in pandas is used for getting the day of the week in numbers. Lets see how to. Get the Day of the week from date in English in pandas; Get the day of the week in number (starting from Monday , Monday = 0 and Sunday =6)

Comments
  • Going to the docs is always the first step if you want to use a datetime method. But from my experience, if you want to use a datetime method then you'll need to start creating datetime.datetime or datetime.date objects so your weekday integers would need to change to datetime objects.
  • Good explanation. I did not know about Calendar library.
  • Thank you! This library is in need of better documentation. I had to look into the code to find out that calendar.day_namehas a __getitem__ method that slices a list with datetime objects representing a week (calendar.day_name._days) and returns a formated string representing the name of the day or a list if you pass in a slice.
  • This is excellent for pandas - reindexing by day after groupping, to have the days in order: data.groupby('day').size().reindex(calendar.day_name).plot.bar() - plot a weekly distribution of your data. (the day column contains the weekday name)
  • Better than hardcoding the daynames into your code. calendar also works with locale so the day names will automatically adjust if you want.
  • This doesn't work - OP wants a weekday of 6 to return Sunday, but this returns Saturday.
  • @OrangeFlash81 thx! now it's much easy since no any offset :D
  • @zondo Thanks, fixed that.
  • If you do it like this and you get a 0 (zero) for Monday, it will return None, which may end up in an Exception. You need to do return days[weekday] if -1 < weekday < len(days) else None in order to be able to return "Monday".