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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,883 questions

51,809 answers

573 users

How to convert days into human-readable years, months and days in C

2 Answers

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

int main() {
    // Difference between Jan 1, 2024 and Mar 27, 2025 (including both days):
    // 1 years 2 months 27 days
    // or 14 months 27 days
    // or 64 weeks 4 days
    // or 452 calendar days

    int days = 452;
    struct tm startDate = {0, 0, 0, 1, 0, 2024 - 1900}; // based on actual days in each month
    struct tm endDate = startDate;
    endDate.tm_mday += days;
    mktime(&endDate);

    int years = endDate.tm_year - startDate.tm_year;
    int months = endDate.tm_mon - startDate.tm_mon;
    int daysLeft = endDate.tm_mday - startDate.tm_mday;

    printf("%d years %d months %d days\n", years, months, daysLeft);
    
    return 0;
}

   
   
/*
run:
  
1 years 2 months 27 days
   
*/

 



answered Jun 27, 2024 by avibootz
0 votes
#include <stdio.h>

void days_to_ymd(int total_days, int *years, int *months, int *days) {
    *years  = total_days / 365;
    total_days %= 365;

    *months = total_days / 30;
    total_days %= 30;

    *days   = total_days;
}

int main(void) {
    int days = 452;
    int y, m, d;

    days_to_ymd(days, &y, &m, &d);

    printf("%d year%s, %d month%s and %d day%s\n",
           y, y == 1 ? "" : "s",
           m, m == 1 ? "" : "s",
           d, d == 1 ? "" : "s");

    return 0;
}


/*
run:

1 year, 2 months and 27 days

*/

 



answered Dec 31, 2025 by avibootz

Related questions

...