问题
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 using Python 2.7.
For example, these dates can help you better understand my problem. Have a yearly calendar at hand.
- input ---> output
- 'Jan 18, 2013' ---> True
- 'Feb 22, 2013' ---> False
- 'Jun 21, 2013' ---> True
- 'Sep 20, 2013' ---> True
I just want to use standard classes provided by the language, like time, datetime, calendar, etc.
I have looked into some answers but I see calculations like addidng/substracting 86400 seconds for every day or even doing comparisons depending on the number of days that a month has. IMHO these are wrong because the libreries in Python already take care of these details so there is no need to reinvent the wheel. Also calendars and dates are complex: leap years, leap seconds, time zones, week numbers, etc. so I think is better to let the libraries solve these tricky details.
Thanks in advance for your help.
回答1:
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
回答2:
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.
回答3:
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())
回答4:
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
回答5:
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.
来源:https://stackoverflow.com/questions/18424467/python-third-friday-of-a-month