Python: third Friday of a month

后端 未结 5 1569
被撕碎了的回忆
被撕碎了的回忆 2021-01-31 21:24

I am a rookie python programmer and I need to write a script to check if a given date (passed as a string in the form \'Month, day year\') is the third Friday of the month. I am

5条回答
  •  广开言路
    2021-01-31 21:26

    This should do it:

    from datetime import datetime 
    
    def is_third_friday(s):
        d = datetime.strptime(s, '%b %d, %Y')
        return d.weekday() == 4 and 15 <= d.day <= 21
    

    Test:

    print is_third_friday('Jan 18, 2013')  # True
    print is_third_friday('Feb 22, 2013')  # False
    print is_third_friday('Jun 21, 2013')  # True
    print is_third_friday('Sep 20, 2013')  # True
    

提交回复
热议问题