Programming Tips - C/C++: Find the day of the week

Date: 2022nov19 Update: 2026sep29 Language: C/C++ Q. C/C++: Find the day of the week A. Here is a function for that:
#include <time.h> // 1=Sunday, 2=Monday, 3=Tuesday, ... int dayOFWeek() { const time_t now = time(NULL); struct tm here; localtime_r(&now, &here); return here.tm_wday + 1; // Add one since wday is zero based }
Full Example Use:
#include <time.h> #include <stdio.h> // (Place the above function here) int main() { const int wday = dayOfWeek(); printf("Today is %d\n", wday); }
Output on a Tuesday:
Today is 3