Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7942675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:02:45+00:00 2026-06-04T00:02:45+00:00

Here is a problem. I have seen many solutions, but no one seems to

  • 0

Here is a problem. I have seen many solutions, but no one seems to be fulfilling the criteria I want…

I want to display the age in this format

20 y(s) 2 m(s) 20 d(s)
20 y(s) 2 m(s)
2 m(s) 20 d(s)
20 d(s)

etc…

I have tried several solutions, but the leap year is causing the problem with me. My unit tests are always being failed because of leap years and no matter how many days come in between, the leap yeas count for extra number of days.

Here is my code….

public static string AgeDiscription(DateTime dateOfBirth)
{
    var today = DateTime.Now;
    var days = GetNumberofDaysUptoNow(dateOfBirth);
    var months = 0;
    var years = 0;
    if (days > 365)
    {
        years = today.Year - dateOfBirth.Year;
        days = days % 365;
    }
    if (days > DateTime.DaysInMonth(today.Year, today.Month))
    {
        months = Math.Abs(today.Month - dateOfBirth.Month);
        for (int i = 0; i < months; i++)
        {
            days -= DateTime.DaysInMonth(today.Year, today.AddMonths(0 - i).Month);
        }
    }

    var ageDescription = new StringBuilder("");

    if (years != 0)
        ageDescription = ageDescription.Append(years + " y(s) ");
    if (months != 0)
        ageDescription = ageDescription.Append(months + " m(s) ");
    if (days != 0)
        ageDescription = ageDescription.Append(days + " d(s) ");

    return ageDescription.ToString();
}

public static int GetNumberofDaysUptoNow(DateTime dateOfBirth)
{
    var today = DateTime.Now;
    var timeSpan = today - dateOfBirth;
    var nDays = timeSpan.Days;
    return nDays;
}

Any ideas???

UPDATE:

I want the difference between the two dates as:

var dateOfBirth = DateTime.Now.AddYears(-20);
string expected = "20 y(s) ";
string actual; // returns 20 y(s) 5 d(s)
actual = Globals.AgeDiscription(dateOfBirth);
Assert.AreEqual(expected, actual);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:02:46+00:00Added an answer on June 4, 2026 at 12:02 am

    Age is pretty tricky. Here’s the relevant excerpts from a struct I use.

    public struct Age
    {
        private readonly Int32 _years;
        private readonly Int32 _months;
        private readonly Int32 _days;
        private readonly Int32 _totalDays;
    
        /// <summary>
        /// Initializes a new instance of <see cref="Age"/>.
        /// </summary>
        /// <param name="start">The date and time when the age started.</param>
        /// <param name="end">The date and time when the age ended.</param>
        /// <remarks>This </remarks>
        public Age(DateTime start, DateTime end)
            : this(start, end, CultureInfo.CurrentCulture.Calendar)
        {
        }
    
        /// <summary>
        /// Initializes a new instance of <see cref="Age"/>.
        /// </summary>
        /// <param name="start">The date and time when the age started.</param>
        /// <param name="end">The date and time when the age ended.</param>
        /// <param name="calendar">Calendar used to calculate age.</param>
        public Age(DateTime start, DateTime end, Calendar calendar)
        {
            if (start > end) throw new ArgumentException("The starting date cannot be later than the end date.");
    
            var startDate = start.Date;
            var endDate = end.Date;
    
            _years = _months = _days = 0;
            _days += calendar.GetDayOfMonth(endDate) - calendar.GetDayOfMonth(startDate);
            if (_days < 0)
            {
                _days += calendar.GetDaysInMonth(calendar.GetYear(startDate), calendar.GetMonth(startDate));
                _months--;
            }
            _months += calendar.GetMonth(endDate) - calendar.GetMonth(startDate);
            if (_months < 0)
            {
                _months += calendar.GetMonthsInYear(calendar.GetYear(startDate));
                _years--;
            }
            _years += calendar.GetYear(endDate) - calendar.GetYear(startDate);
    
            var ts = endDate.Subtract(startDate);
            _totalDays = (Int32)ts.TotalDays;
        }
    
        /// <summary>
        /// Gets the number of whole years something has aged.
        /// </summary>
        public Int32 Years
        {
            get { return _years; }
        }
    
        /// <summary>
        /// Gets the number of whole months something has aged past the value of <see cref="Years"/>.
        /// </summary>
        public Int32 Months
        {
            get { return _months; }
        }
    
        /// <summary>
        /// Gets the age as an expression of whole months.
        /// </summary>
        public Int32 TotalMonths
        {
            get { return _years * 12 + _months; }
        }
    
        /// <summary>
        /// Gets the number of whole weeks something has aged past the value of <see cref="Years"/> and <see cref="Months"/>.
        /// </summary>
        public Int32 Days
        {
            get { return _days; }
        }
    
        /// <summary>
        /// Gets the total number of days that have elapsed since the start and end dates.
        /// </summary>
        public Int32 TotalDays
        {
            get { return _totalDays; }
        }
    
        /// <summary>
        /// Gets the number of whole weeks something has aged past the value of <see cref="Years"/> and <see cref="Months"/>.
        /// </summary>
        public Int32 Weeks
        {
            get { return (Int32) Math.Floor((Decimal) _days/7); }
        }
    
        /// <summary>
        /// Gets the age as an expression of whole weeks.
        /// </summary>
        public Int32 TotalWeeks
        {
            get { return (Int32) Math.Floor((Decimal) _totalDays/7); }
        }
    }
    

    Here’s an example unit test that passes:

        [Test]
        public void Should_be_exactly_20_years_old()
        {
            var now = DateTime.Now;
            var age = new Age(now.AddYears(-20), now);
    
            Assert.That(age, Has.Property("Years").EqualTo(20)
                .And.Property("Months").EqualTo(0)
                .And.Property("Days").EqualTo(0));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen this problem arise in many different circumstances and would like to
There are many questions about this PersistenceException, but I have not seen some, where
I have tried many things, but I'm still not getting this to work. Here's
Here's my problem - I have some code like this: <mx:Canvas width=300 height=300> <mx:Button
Here is the problem: We have all of our development under subversion, but our
I've seen this problem come up a lot, but never adequately handled, and I
I've seen similar questions on here but I can't seem to apply the solutions
Ok I have seen many posts on this script. Which can be found..... http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
I've seen many questions about this, but i've never really got the answer that
I have seen many questions raised around PartialViews and Javascript: the problem is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.