#include <iostream>
#include <ctime>
bool isNthWeekday(int year, int month, int day, int N) {
std::tm t = {};
t.tm_year = year - 1900;
t.tm_mon = month - 1;
t.tm_mday = 1;
std::mktime(&t); // normalize and compute weekday
int firstWeekday = t.tm_wday; // 0=Sun, 1=Mon, ..., 6=Sat
// weekday of the given date
std::tm t2 = {};
t2.tm_year = year - 1900;
t2.tm_mon = month - 1;
t2.tm_mday = day;
std::mktime(&t2);
int weekday = t2.tm_wday;
// Compute which occurrence this weekday is
// Example: if first weekday is Wed (3) and today is Mon (1):
// offset = (1 - 3 + 7) % 7 = 5
// first Monday is on day 6
int offset = (weekday - firstWeekday + 7) % 7;
int firstOccurrenceDay = 1 + offset;
int occurrenceNumber = 1 + (day - firstOccurrenceDay) / 7;
return occurrenceNumber == N;
}
int main() {
int year = 2026, month = 1, day = 14; // Example: Jan 14, 2025
int N = 2; // Is it the 2rd weekday?
if (isNthWeekday(year, month, day, N))
std::cout << "Yes, it is the " << N << "rd occurrence.\n";
else
std::cout << "No, it is not.\n";
}
/*
run:
Yes, it is the 2rd occurrence.
*/