C#格式化年龄 - 关于天,周,月 - 年

时间:2011-02-04 18:58:33

标签: c# .net visual-studio formatting

我正在研究一些医疗软件,我需要根据以下规则以非常具体的方式输出所有年龄段:

 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)

使用C#我试图找到一个简单的方法,只使用一个人的出生日期,任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:7)

昨天我正在做类似的事情,但是这样的事情应该适合你的需要:(假设7天周,31天月,365天等等。

修订方法: (根据Bob的建议修正)

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");
    }

以前的方法:

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');
        }

希望这有帮助!

答案 1 :(得分:3)

可以从其他DateTime中减去DateTime类型,从而生成表示间隙的TimeSpan。试试这个:

var timeAlive = DateTime.Today - dateOfBirth.Date;

然后,查看timeAlive的天,月和年(将天数除以7),并相应地格式化。

答案 2 :(得分:2)

以下内容不对日/月或年进行任何假设 不利的一面是,它不兼容Y3K。

    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");
    }

答案 3 :(得分:0)

您可以通过简单的减法获得表示用户当前年龄的对象:

TimeSpan age = DateTime.Now - dateOfBirth;

然后,只需要做一堆if子句

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

您应该能够弄清楚如何进行格式化。