Get Day name from Weekday int

后端 未结 8 1116
梦如初夏
梦如初夏 2020-12-14 05:14

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?

H

相关标签:
8条回答
  • 2020-12-14 05:56

    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"
    
    0 讨论(0)
  • 2020-12-14 06:00

    It's very easy:

    week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    week[weekday]
    
    0 讨论(0)
  • 2020-12-14 06:04

    You can use built in functions for that for ex suppose you have to find day according to the specific date.

    import calendar
    
    month,day,year = 9,19,1995
    ans =calendar.weekday(year,month,day)
    
    print(calendar.day_name[ans])
    

    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

    for more reference -https://docs.python.org/2/library/calendar.html#calendar.weekday

    0 讨论(0)
  • 2020-12-14 06:12

    You can use index number like this:

    days=["sunday","monday"," Tuesday", "Wednesday" ,"Thursday", "Friday", "Saturday"]
    def date(i):
    
        return days[i]
    print (date(int(input("input index.  "))))
    
    0 讨论(0)
  • 2020-12-14 06:14

    You can create your own list and use it with format.

    import datetime
    
    days_ES = ["Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"]
    
    t = datetime.datetime.now()
    f = t.strftime("{%w} %Y-%m-%d").format(*days_ES)
    
    print(f)
    

    Prints:

    Lunes 2018-03-12
    
    0 讨论(0)
  • 2020-12-14 06:15

    I have been using calendar module:

    import calendar
    calendar.day_name[0]
    'Monday'
    
    0 讨论(0)
提交回复
热议问题