How do I get the day of week given a date?

前端 未结 26 1770
迷失自我
迷失自我 2020-11-22 04:40

I want to find out the following: given a date (datetime object), what is the corresponding day of the week?

For instance, Sunday is the first day, Mond

26条回答
  •  萌比男神i
    2020-11-22 05:10

    Below is the code to enter date in the format of DD-MM-YYYY you can change the input format by changing the order of '%d-%m-%Y' and also by changing the delimiter.

    import datetime
    try:
        date = input()
        date_time_obj = datetime.datetime.strptime(date, '%d-%m-%Y')
        print(date_time_obj.strftime('%A'))
    except ValueError:
        print("Invalid date.")
    

提交回复
热议问题