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

发布于 2024-10-15 16:47:07 字数 318 浏览 4 评论 0原文

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

 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# 我试图找到一种简单的方法来做到这一点,只需使用一个人的出生日期,任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

会傲 2024-10-22 16:47:07

我昨天正在研究类似的东西,但是这样的东西应该适合您的需求:(假设每周 7 天,每月 31 天,每年 365 天等。

修订方法: (根据鲍勃的建议修复)

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

希望这有帮助!

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!

别闹i 2024-10-22 16:47:07

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

var timeAlive = DateTime.Today - dateOfBirth.Date;

然后,查看 timeAlive 的日、月和年(将日除以 7 得到周),并相应地设置格式。

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.

狼亦尘 2024-10-22 16:47:07

以下内容不对天/月或年做出任何假设。
缺点是它不兼容 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");
    }

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");
    }
谎言月老 2024-10-22 16:47:07

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

TimeSpan age = DateTime.Now - dateOfBirth;

然后只需执行一堆 if 子句即可

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文