Skip to content

Commit 97f7f44

Browse files
add date_to_weekday finder method (TheAlgorithms#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
1 parent 265c7ce commit 97f7f44

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Diff for: other/date_to_weekday.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from calendar import day_name
2+
from datetime import datetime
3+
4+
5+
def date_to_weekday(inp_date: str) -> str:
6+
"""
7+
It returns the day name of the given date string.
8+
:param inp_date:
9+
:return: String
10+
>>> date_to_weekday("7/8/2035")
11+
'Tuesday'
12+
>>> date_to_weekday("7/8/2021")
13+
'Saturday'
14+
>>> date_to_weekday("1/1/2021")
15+
'Friday'
16+
"""
17+
day, month, year = [int(x) for x in inp_date.split("/")]
18+
if year % 100 == 0:
19+
year = "00"
20+
new_base_date: str = f"{day}/{month}/{year%100} 0:0:0"
21+
date_time_obj: datetime.date = datetime.strptime(new_base_date, "%d/%m/%y %H:%M:%S")
22+
out_put_day: int = date_time_obj.weekday()
23+
return day_name[out_put_day]
24+
25+
26+
if __name__ == "__main__":
27+
print(date_to_weekday("1/1/2021"), end=" ")

0 commit comments

Comments
 (0)