add date_to_weekday finder method (#4599)

* add date_to_weekday finder method

* reformat date_to_weekday method

* remove time

* remove hardcode weekdays list

* fix return type error

* fixing fail issue

* Finding the test failing issue

* after testing the pre-commit in local environment
This commit is contained in:
SURYAPRATAP SINGH SURYAVANSHI 2021-08-18 17:35:41 +05:30 committed by GitHub
parent af0810fca1
commit 9cb5760e89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

27
other/date_to_weekday.py Normal file
View File

@ -0,0 +1,27 @@
from calendar import day_name
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, 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} 0:0:0"
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_name[out_put_day]
if __name__ == "__main__":
print(date_to_weekday("1/1/2021"), end=" ")