Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,709 questions

55,473 answers

573 users

How to compute the day of the week for January 1st of any given year in C

1 Answer

0 votes
#include <stdio.h>
#include <time.h>

/*
    This program finds the day of the week for January 1st of a given year.

    Approach:
    ---------
    We use the C standard library's <time.h> facilities, which include:
        - struct tm : a calendar date/time structure
        - mktime()  : converts struct tm into a normalized time value and
                      fills in fields like tm_wday (day of week)

    Why this is efficient:
    ----------------------
    mktime() uses the system's optimized calendar routines, avoiding manual
    arithmetic or algorithms. It is portable, reliable, and efficient.

    tm_wday meaning:
        0 = Sunday
        1 = Monday
        2 = Tuesday
        3 = Wednesday
        4 = Thursday
        5 = Friday
        6 = Saturday
*/

// Convert tm_wday (0..6) to a readable weekday name
const char* weekday_name(int wday) {
    switch (wday) {
        case 0: return "Sunday";
        case 1: return "Monday";
        case 2: return "Tuesday";
        case 3: return "Wednesday";
        case 4: return "Thursday";
        case 5: return "Friday";
        case 6: return "Saturday";
        default: return "Unknown";
    }
}

// Compute weekday of January 1st for a given year
const char* jan1_weekday(int year) {
    struct tm date = {0};

    date.tm_year = year - 1900;   // years since 1900
    date.tm_mon  = 0;             // January (0-based)
    date.tm_mday = 1;             // January 1st

    // mktime normalizes the date and computes tm_wday
    mktime(&date);

    return weekday_name(date.tm_wday);
}

int main(void) {
    int year = 2026;  

    const char* result = jan1_weekday(year);
    printf("January 1st, %d falls on a %s.\n", year, result);

    return 0;
}


/*
run:

January 1st, 2026 falls on a Thursday.

*/

 



answered Jul 11 by avibootz

Related questions

...