Im a complete noob in python and coding in general.... I was making a program to calculate the date and day of the next day of the week.... Here, a is the day of the month, b is the month c is the year d is the day of the week. And I wanted to know if we could leverage this to generate the date of a given day without having to enter the present day?
Asked
Active
Viewed 30 times
-1
-
Can you provide an example input and expected output? Do you mean to find the date that is x days from today? – buran Jun 23 '22 at 17:58
-
Its more of the particular day of a random date... for example the 10th of may of 2021 which was on a Monday.... I need it to accept the inputs and display that its Monday – Dhasvanth .M.G Jun 23 '22 at 18:01
-
1Check https://stackoverflow.com/q/9847213/4046632 – buran Jun 23 '22 at 18:02
2 Answers
0
Try this:
import datetime
a = int(input('Enter a day: ')) # 10
b = int(input('Enter a month: ')) # 5
c = int(input('Enter a year: ')) # 2021
d = datetime.datetime(c, b, a).weekday() # 0
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
print(f'{a}/{b}/{c} - {days[d]}') # 10/5/2021 - Mon
Output:
Enter a day: 10
Enter a month: 5
Enter a year: 2021
10/5/2021 - Mon

The Thonnu
- 3,578
- 2
- 8
- 30
0
I'm not completely sure what you're asking, but if you want to get the weekday (Monday/Tuesday/etc..) from any given date, you could use the variables to construct a date
object. You can get the weekday from that as an integer 0-6 with the weekday()
method or you could print the weekday name using the strftime()
method.
Example:
import datetime
# day
a = 23
# month
b = 6
# year
c = 2022
input_day = datetime.date(day=a, month=b, year=c)
d = input_day.weekday()
print(d)
# Output: 3
d = input_day.strftime('%A')
print(d)
# Output: 'Thursday'
For more info: https://docs.python.org/3/library/datetime.html#date-objects

Sam Zuk
- 309
- 1
- 3
- 14