How to convert number of days to years, months and days in C++

1 Answer

0 votes
#include <iostream>
 
void print_ymd(int total_days) {
    int years, months, days;
 
    years = total_days / 365;
    total_days = total_days - (365 * years);
    months = total_days / 30;
    days = total_days - (months * 30);
 
    std::cout << "years = " << years << "\n";
    std::cout << "months = " <<  months << "\n";
    std::cout << "days = " <<  days << "\n";
}
 
int main() {
    int total_days = 438;
 
    print_ymd(total_days);
}
 
 
 
 
/*
run:
 
years = 1
months = 2
days = 13
 
*/

 



answered Sep 25, 2021 by avibootz
edited May 6, 2023 by avibootz
...