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

前端 未结 26 1767
迷失自我
迷失自我 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:09

    datetime library sometimes gives errors with strptime() so I switched to dateutil library. Here's an example of how you can use it :

    from dateutil import parser
    parser.parse('January 11, 2010').strftime("%a")
    

    The output that you get from this is 'Mon'. If you want the output as 'Monday', use the following :

    parser.parse('January 11, 2010').strftime("%A")
    

    This worked for me pretty quickly. I was having problems while using the datetime library because I wanted to store the weekday name instead of weekday number and the format from using the datetime library was causing problems. If you're not having problems with this, great! If you are, you cand efinitely go for this as it has a simpler syntax as well. Hope this helps.

提交回复
热议问题