Python How to Find Day of the Week
DAY of the week Python code you will know here. One is from Calendar, and another one is from a function. Here, I have given my ideas on a better method. Also, the result you can get in a tuple. You might also like Attitude is Everything That Changes Your Life.
from datetime import datetime
import calendar
dt = datetime.today()
dn = dt.weekday()
print("Today is day number: {0}".format(dn))
print("Today is a {0}".format(calendar.day_name[dn]))
Here, the print uses string.format method to print output.
Instead of directly using DateTime, you can write a function - that creates a tuple with Day of week and Day name.
Day of Week and Day_Name
- Monday 0
- Tuesday 1
- Wednesday 2
- Thursday 3
- Friday 4
- Saturday 5
- Sunday 6
![]() |
Python: Day of Week How to Calculate |
1. Python Logic to get Day of Week from Calendar
import calendar
dt = datetime.today()
dn = dt.weekday()
print("Today is day number: {0}".format(dn))
print("Today is a {0}".format(calendar.day_name[dn]))
Check out the best example to use this method.
The Output
![]() |
The result from the first logic |
Instead of directly using DateTime, you can write a function - that creates a tuple with Day of week and Day name.
2. Python Logic to get Day and Day_name
User defined function
from datetime import datetime
from datetime import timedelta
import calendar
def DayOfWeek(d=None):
if d == None:
d = datetime.today()
dn = d.weekday()
return (dn, calendar.day_name[dn])
print(DayOfWeek())
d2 = datetime.today() + timedelta(1)
print(DayOfWeek(d2))
from datetime import timedelta
import calendar
def DayOfWeek(d=None):
if d == None:
d = datetime.today()
dn = d.weekday()
return (dn, calendar.day_name[dn])
print(DayOfWeek())
d2 = datetime.today() + timedelta(1)
print(DayOfWeek(d2))
Here, DayOfWeek is a function. You assigned None to d. In the IF, for 'd' and 'dn', you have assigned values. When you Print executes, it returns two values as a tuple.
An object is simply a collection of data (variables) and methods (functions) that act on those data. Similarly, a class is a blueprint for that object.
The Output
![]() |
The result from the second logic |
Keep Reading
Comments
Post a Comment
Thanks for your message. We will get back you.