### How to find the second Tuesday in October of a given year in C++?

For example, given 2019, October, return 12 since the second Tuesday on October 2019 is 12th. I know how to do it in java. I want to implement it in C++, but I do not know what libraries I should use.

The following is the Java code:
public int date(int year) {
LocalDate start = LocalDate.of(year, 10, 1);

if (start.getDayOfWeek() == DayOfWeek.TUESDAY)
return start.plusWeeks(1).getDayOfMonth();

}

Find what week day the first of the month was and work from that.

 ``1234567891011121314151617181920212223`` ``````#include #include using namespace std; const char * weekday[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; int getFirstWeekDayOfMonth( int year, int month ) { tm TM = { 0, 0, 0, 1, month - 1, year - 1900 }; mktime( &TM ); return TM.tm_wday; } int main () { int year, month; cout << "Enter year: "; cin >> year; cout << "Enter month (1-12): "; cin >> month; int first = getFirstWeekDayOfMonth( year, month ); cout << "The first day of the month was a " << weekday[first] << '\n'; }``````

March this year:

 ```Enter year: 2019 Enter month (1-12): 3 The first day of the month was a Friday```
Last edited on
Second tuesday in October 2019 is the 8th (first tuesday is the 1st)

It's really neat in C++20:

 ``1234567`` ``````#include #include using namespace std::chrono; int main() { auto d = year_month_day{ Tuesday[2] / October / 2019y } ; // 2019-10-08 std::cout << d.day() << '\n'; // 8 }``````

(until C++20 comes to compiler near you, you can use date.h: live demo https://wandbox.org/permlink/IJfTLWVZz2ASdOjo

Edit: to make it match that Java method signature in the original post,

 ``123`` ``````int date(int y) { return (unsigned)year_month_day{ Tuesday[2] / October / year{y} }.day(); }``````

 ``1234567891011121314151617181920212223242526272829`` ``````#include #include using namespace std; const char * weekday[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; int getFirstWeekDayOfMonth( int year, int month ) { tm TM = { 0, 0, 0, 1, month - 1, year - 1900 }; mktime( &TM ); return TM.tm_wday; } int getUmpteenthDayOfMonth( int year, int month, int day, int n ) { int day1 = 1 + day - getFirstWeekDayOfMonth( year, month ); if ( day1 < 1 ) day1 += 7; return day1 + ( n - 1 ) * 7; } int main () { int n = 2; // The particular day count int day = 2; // Tuesday int month = 10; // October int year; cout << "Enter year: "; cin >> year; cout << "Required day = " << getUmpteenthDayOfMonth( year, month, day, n ) << '\n'; }``````
 ```Enter year: 2019 Required day = 8```