简体   繁体   中英

C# Formatting Age - Regarding Days, Weeks , Months - Years

I am working on some medical software and I am required to output all ages in a very specific manner, based on the following rules:

 If under 6 Weeks old :  ###D (Number of Days)
If under 6 Months old :  ###W (Number of Weeks)
 If under 2 Years old :  ###M (Number of Months)
 If above 2 Years old :  ###Y (Number of Years)

Using C# I am trying to find a simple method of doing this just using a Person's Date of Birth, any help would be greatly appreciated.

I was working on something similar yesterday, but something like this should suit your needs: ( assuming 7 day weeks, 31 day months, 365 day years etc. )

Revised Method : (Fixed as per Bob's suggestions)

public static string ConvertAge(DateTime dob)
    {
        DateTime today = DateTime.Today;
        string fmt = "{0:0##}{1}";

        //Greater than 2 Years old - Ouput Years
        if (dob <= today.AddYears(-2)) 
            return string.Format(fmt, (dob.DayOfYear <= today.DayOfYear) ? 
            (today.Year - dob.Year) : (today.Year - dob.Year)-1, "Y");
        //Less than 2 Years - Output Months
        if (dob < today.AddMonths(-2)) 
            return string.Format(fmt, (dob.DayOfYear <= today.DayOfYear) ? 
            (today.Year - dob.Year) * 12 + (today.Month - dob.Month) : 
            ((today.Year - dob.Year) * 12 + (today.Month - dob.Month))-1 , "M");
        //Less than 2 Months - Output Weeks
        if (dob < today.AddDays(-2 * 7)) 
            return string.Format(fmt, (today - dob).Days / 7, "W");
        //Less than 2 Weeks - Output Days
        return string.Format(fmt, (today - dob).Days, "D");
    }

Previous Method :

public string ConvertAge(DateTime dateOfBirth)
        {
            int daysOld = (DateTime.Now - dateOfBirth).Days;

            //Age < 6 Weeks
            if (daysOld < (6 * 7)) 
                return String.Format("{0:0##}{1}", daysOld, 'D'); 
            //Age < 6 Months
            else if (daysOld < (6 * 31)) 
                return String.Format("{0:0##}{1}", daysOld/7, 'W');
            //Age < 2 Years
            else if (daysOld < (2 * 365)) 
                return String.Format("{0:0##}{1}", daysOld / 31, 'M');
            //Age >= 2 Years
            else 
                return String.Format("{0:0##}{1}", daysOld / 365, 'Y');
        }

Hope this helps!

A DateTime type can be subtracted from other DateTimes, resulting in a TimeSpan representing the gap. Try this:

var timeAlive = DateTime.Today - dateOfBirth.Date;

Then, look at the Days, Months and Years (divide Days by 7 for Weeks) of timeAlive, and format accordingly.

The following makes no assumptions about days/months or year.
On the downside, it is not Y3K compatible.

    public static string GetAge (DateTime dob) {
        DateTime today = DateTime.Now;
        string fmt = "{0:0##}{1}";

        if (dob < today.AddYears(-2)) return string.Format(fmt, today.Year - dob.Year, "Y");
        if (dob < today.AddMonths(-6))return string.Format(fmt, (today.Year - dob.Year)*12 + (today.Month - dob.Month), "M");
        if (dob < today.AddDays(-6 * 7)) return string.Format(fmt, (today - dob).Days/7, "W");
        return string.Format(fmt, (today - dob).Days, "D");
    }

You can get an object representing the user's current age with a simple subtraction:

TimeSpan age = DateTime.Now - dateOfBirth;

And then it's just a matter of doing a bunch of if clauses

if (age.TotalDays < 6 * 7) // 6 weeks
    // ...
else if (age.TotalDays < 6 * 30) // 6 months
    // ...
// et cetera

You should be able to figure out how to do your formatting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM