Python: third Friday of a month

后端 未结 5 1559
被撕碎了的回忆
被撕碎了的回忆 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
    
    0 讨论(0)
  • 2021-01-31 21:33

    Yet another way to accomplish this... using integer division...

    import datetime
    
    def is_date_the_nth_friday_of_month(nth, date=None):
        #nth is an integer representing the nth weekday of the month
        #date is a datetime.datetime object, which you can create by doing datetime.datetime(2016,1,11) for January 11th, 2016
    
        if not date:
            #if date is None, then use today as the date
            date = datetime.datetime.today()
    
        if date.weekday() == 4:
            #if the weekday of date is Friday, then see if it is the nth Friday
            if (date.day - 1) // 7 == (nth - 1):
                #We use integer division to determine the nth Friday
                #if this integer division == 0, then date is the first Friday, 
                # if the integer division is...
                #   1 == 2nd Friday
                #   2 == 3rd Friday
                #   3 == 4th Friday
                #   4 == 5th Friday
                return True
    
        return False
    
    0 讨论(0)
  • 2021-01-31 21:35

    you can use something like this to get third fridday of the month (current month), and then simply compare that third_friday with your day (by year, month, day)

    from datetime import datetime, timedelta
    import calendar
    
    now = datetime.now()
    first_day_of_month = datetime(now.year, now.month, 1)
    first_friday = first_day_of_month + timedelta(days=((4-calendar.monthrange(now.year,now.month)[0])+7)%7)
    # 4 is friday of week
    third_friday = first_friday + timedelta(days=14)
    

    Hope this helps.

    0 讨论(0)
  • 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())
    
    0 讨论(0)
  • 2021-01-31 21:41

    This is the nicest I've found so far.

    from datetime import date
    from dateutil.relativedelta import relativedelta, FR
    
    def get_third_fri_of_mth(dt):
        print (dt + relativedelta(day=1, weekday=FR(3)))
    
    get_third_fri_of_mth(date(2019, 1, 30))
    get_third_fri_of_mth(date(2019, 6, 4))
    

    Relative delta replaces the day in the date you specify by day = 1. From this new date, weekday=FR(3) specifies the 3rd friday.

    0 讨论(0)
提交回复
热议问题