Code to obtain the day of the week when entering Python date

Asked 2 years ago, Updated 2 years ago, 16 views

If you enter a date in Python (yyyy.mm.dd) and enter April 1, 2021 in the code to obtain the day of the week, it will print Friday, not Thursday. We've done a lot of output and it's normal until February 28, 1900, and recognizing February 29, 1900, we've confirmed that the day of the week will be +1 from March 1, 1900. Can you tell me why and also how to fix this?

year=int(input(">> year : "))
month=int(input(">> month : "))
date=int(input(">> date : "))

days=0

month_days=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

for i in range(1, year):
         if i%4==0 or i%400==0 and i%100!=0:
             days=days+366
         else:
             days=days+365

for i in range(1, month):
    days=days+month_days[i-1]

if month>=3:
    if year%4==0 or year%400==0 and year%100!=0:
        days=days+1
    else:
        days=days

days=days+date

print(days)

if days%7==1:
    print(">>{} in {}} is Monday."format(year, month, date))
elif days%7==2:
    print(">>{} in {}} is Tuesday."format(year, month, date))
elif days%7==3:
    print(">>{}/{}/{} is Wednesday."format(year, month, date))
elif days%7==4:
    print(">>{} in {}} is Thursday."format(year, month, date))
elif days%7==5:
    print(">>{}/{}/{} is Friday."format(year, month, date))
elif days%7==6:
    print(">>{} in {}} is Saturday."format(year, month, date))
elif days%7==0:
    print(">>{} in {} is Sunday."format(year, month, date))

python

2022-09-20 17:21

1 Answers

After coding it myself, there are two tricky ideas.

Because, for example,

From 1 January A.D. to 16 July A.D.:

It's because it works.

Please understand this part. This is important because you're approaching it by finding the difference in the number of days (diff) to get the days of the week. I was wondering why it wasn't working, but when I thought about it, it became like this.

According to the definition :

So you just have to do simple multiplication and addition with these codes.

import math

diff = year * 365
diff = diff + int(math.floor(year / 4))
diff = diff - int(math.floor(year / 100))
diff = diff + int(math.floor(year / 400))

Anyway, so the code I wrote is as follows.
Insert the appropriate year, month, and day factors and run them to check them out with Python's datetime library.

import math
import datetime

def getWeekdayStr(weekdayIndex) :
    weekdays = ['mon', 'tue', 'wed', 'thr', 'fri', 'sat', 'sun']
    return weekdays[weekdayIndex]

def getWeekday(year, month, day) :
    monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    year = year - 1
    month = month - 1
    diff = year * 365
    diff = diff + int(math.floor(year / 4))
    diff = diff - int(math.floor(year / 100))
    diff = diff + int(math.floor(year / 400))
    for i in range(0, month) :
        diff = diff + monthDays[i]
    diff = diff + day
    Diff = diff - 1 # Since we are looking for the difference, we need to subtract 1 to operate the mod operation below accurately
    print('weekday seems like: ' + str(diff % 7))
    return getWeekdayStr(diff % 7)

print(getWeekday(2017, 7, 1))
print('weekday really is: ' + str(datetime.date(2017, 7, 1).weekday()))


2022-09-20 17:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.