Python: third Friday of a month

后端 未结 5 1565
被撕碎了的回忆
被撕碎了的回忆 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:39

    If you're trying to find the expiration of an option, you could use something like this

    from datetime import datetime
    import calendar
    
    def option_expiration(date):
        day = 21 - (calendar.weekday(date.year, date.month, 1) + 2) % 7
        return datetime(date.year, date.month, day)
    
    print option_expiration(datetime.today())
    

提交回复
热议问题