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.

40,023 questions

51,974 answers

573 users

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

2 Answers

0 votes
using System;

public static class DayConverter
{
    public static (int Years, int Months, int Days) SplitDays(int totalDays) {
        DateTime start = new DateTime(1970, 1, 1);
        DateTime end   = start.AddDays(totalDays);

        int years  = end.Year  - start.Year;
        int months = end.Month - start.Month;
        int days   = end.Day   - start.Day;

        // Normalize negative days
        if (days < 0) {
            months--;
            DateTime prevMonth = end.AddMonths(-1);
            days += DateTime.DaysInMonth(prevMonth.Year, prevMonth.Month);
        }

        // Normalize negative months
        if (months < 0) {
            months += 12;
            years--;
        }

        return (years, months, days);
    }
    public static void Main(string[] args)
    {
        var result = DayConverter.SplitDays(452);
        
        Console.WriteLine($"{result.Years} years, {result.Months} months, {result.Days} days");
    }
}



/*
run:

1 years, 2 months, 28 days

*/

 



answered Dec 31, 2025 by avibootz
0 votes
using System;

public static class DayConverterProgram
{
    public static (int Years, int Months, int Days) DayConverter(int totalDays) {
        int years  = totalDays / 365;
        totalDays %= 365;
    
        int months = totalDays / 30;
        totalDays %= 30;
    
        int days   = totalDays;
    
        return (years, months, days);
    }

    public static void Main(string[] args)
    {
        var result = DayConverter(452);
        
        Console.WriteLine($"{result.Years} years, {result.Months} months, {result.Days} days");
    }
}



/*
run:

1 years, 2 months, 27 days

*/

 



answered Dec 31, 2025 by avibootz

Related questions

...