Find day of week from month and year

J

John Machin

Terry said:
Laguna said:
I want to find the expiration date of stock options (3rd Friday of the
month) for an any give month and year.

From year and month (and day=1) get the day of the week (n in [0,6]) of the
first of the month using some version of the the standard formula (see
below) and look up the third friday date in a precalculated 7-element list,
or, with n=0 on Saturday, 3rd Friday is 21-n

Here is a translation of the guts of a 30-year-old Basic program:

def friday3(m,y): # ints
if m <= 2:
m += 12
y -= 1
d = 1
n = d + 2*m + int(.6*(m+1)) + y + y//4 - y//100 + y//400 + 2

Some simplification is possible:
>>> [2*m + int(.6*(m+1)) for m in range(15)] [0, 3, 5, 8, 11, 13, 16, 18, 21, 24, 26, 29, 31, 34, 37]
>>> [(13*m+3)//5 for m in range(15)] [0, 3, 5, 8, 11, 13, 16, 18, 21, 24, 26, 29, 31, 34, 37]
>>>

n = int((n/7.0- n//7)*7.0 + .5)

n %= 7
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top