Skip to content

add date_to_weekday finder method #4599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 18, 2021
35 changes: 35 additions & 0 deletions other/date_to_weekday.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from datetime import datetime


def date_to_weekday(inp_date: str) -> str:
"""
It returns the day name of the given date string.
:param inp_date:
:return: String
>>> date_to_weekday("7/8/2035")
'Tuesday'
>>> date_to_weekday("7/8/2021")
'Saturday'
>>> date_to_weekday("1/1/2021")
'Friday'
"""
day_list: list = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have pretty module in python so we don't need to hardcode weekdays import calendar;calendar.day_name

"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
]
day, month, year = [int(x) for x in inp_date.split("/")]
if year % 100 == 0:
year = "00"
new_base_date: str = f"{day}/{month}/{year%100} 23:15:59"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need a time here.

date_time_obj: datetime.date = datetime.strptime(new_base_date, "%d/%m/%y %H:%M:%S")
out_put_day: int = date_time_obj.weekday()
return day_list[out_put_day]


if __name__ == "__main__":
print(date_to_weekday("1/1/2021"), end=" ")