Stack Exchange
Calculate how many Monday the 1st between two dates
Is it possible to calculate how many Monday the 1st are between two dates?
I wrote two functions to find how many days are between two dates, using C++:
int rdn(int y, int m, int d) {
    if (m < 3) { y--, m += 12; };
    return 365*y + y/4 - y/100 + y/400 + (153*m - 457)/5 + d - 306;
}

int days = rdn(2015, 01, 01) - rdn(2015, 12, 31);

And using Python:
def days_between(d1, d2):
    d1 = datetime.strptime(d1, "%Y-%m-%d")
    d2 = datetime.strptime(d2, "%Y-%m-%d")
    return abs((d2 - d1).days)

print(days_between('2015-01-01', '2015-12-31'))

But I don't understand how to update my function to calculate Monday the 1st.

What have you tried? There are perpetual calendars (https://en.wikipedia.org/wiki/Perpetual_calendar)  you could use. Isn't this just a matter of counting them?

If you know what weekday is the first of January, then determining the  weekday for any date is trivial. Actually, if you know any weekday in the current year, it's trivial to compute the weekday for any other day that year. You can easily extend that to any number of years into the past or future. See http://www.mischel.com/diary/2005/08/08.htm.

I'm voting to close this question as off-topic because this is ongoing programming competition: https://www.hackerrank.com/contests/w17/challenges/count-fridays-the-13-th , where the solutions would be available in 7 days. The person does not care about knowledge, only about getting points.

@SalvadorDali I solved problem using calendar module in Python. I'm novice in algorithms, and I learn. Try to solve the hackerrank problem using answer to this question. You can? No. And, think head and read topic carefully, before. Thank you so much.

@SalvadorDali I can show you my solution. You will not find a single line of similar code.

You can learn without cheating. What stops you from waiting till the end of the competition and looking other's solution and explanation? Or will you learn worse if you will wait a couple of days? But no, you want points on the leaderboard, no matter what. BTW, this is not your first question from HR competition asked during the competition. Try to solve the hackerrank problem using answer to this question. You can? No. It is straight forward, change 1-st to 13th Mon to Fr. I do not need your solution, just stop cheating.

This algorithm is a loop that examines the weekday of the 1st of each month from the start date (inclusive) to the end date (inclusive), and counts the ones which fall on a Monday:
import datetime

def num_monday_the_first_between(start, end):
    y, m, d = start.year, start.month, 1
    count = 0
    while (y, m, d) <= (end.year, end.month, end.day):
        if datetime.date(y, m, d).isoweekday() == 1:
            count += 1
        m += 1
        if m == 13:
            m = 1
            y += 1
    return count


print(num_monday_the_first_between(datetime.date(2015,1,1), datetime.date(2015,12,31)))
# Result: 1 (June)


good straightforward answer :) even if it is not the shortest it could possibly be

Please, remove your answer, because this question from ongoing programming competition.
