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

前端 未结 26 1990
迷失自我
迷失自我 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条回答
  •  生来不讨喜
    2020-11-22 05:07

    If you're not solely reliant on the datetime module, calendar might be a better alternative. This, for example, will provide you with the day codes:

    calendar.weekday(2017,12,22);
    

    And this will give you the day itself:

    days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
    days[calendar.weekday(2017,12,22)]
    

    Or in the style of python, as a one liner:

    ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"][calendar.weekday(2017,12,22)]
    

提交回复
热议问题