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
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"
It's very easy:
week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
week[weekday]
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
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. "))))
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
I have been using calendar module:
import calendar
calendar.day_name[0]
'Monday'