From 9cb5760e895179f8aaa97dd577442189064c724d Mon Sep 17 00:00:00 2001 From: SURYAPRATAP SINGH SURYAVANSHI <67123991+suryapratapsinghsuryavanshi@users.noreply.github.com> Date: Wed, 18 Aug 2021 17:35:41 +0530 Subject: [PATCH] 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 --- other/date_to_weekday.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 other/date_to_weekday.py diff --git a/other/date_to_weekday.py b/other/date_to_weekday.py new file mode 100644 index 000000000..bb17130c0 --- /dev/null +++ b/other/date_to_weekday.py @@ -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=" ")