C# 从日期时间计算年龄(以年为单位)。月格式?

C# 从日期时间计算年龄(以年为单位)。月格式?,c#,algorithm,C#,Algorithm,有没有人用c#的算法来精确地计算给定日期时间的年龄,格式是Years.Months 例如 出生日期:1988年9月6日 答复:23.4 出生日期:1991年3月31日 答复:20.10 出生日期:1991年2月25日 答复:20.11 谢谢您可以很容易地做到这一点: using System; using NodaTime; class Test { static void Main() { ShowAge(1988, 9, 6); Show

有没有人用c#的算法来精确地计算给定日期时间的年龄,格式是Years.Months

例如

  • 出生日期:1988年9月6日
  • 答复:23.4

  • 出生日期:1991年3月31日

  • 答复:20.10

  • 出生日期:1991年2月25日

  • 答复:20.11
谢谢

您可以很容易地做到这一点:

using System;
using NodaTime;

class Test
{
    static void Main()
    {
        ShowAge(1988, 9, 6);
        ShowAge(1991, 3, 31);
        ShowAge(1991, 2, 25);
    }

    private static readonly PeriodType YearMonth =
        PeriodType.YearMonthDay.WithDaysRemoved();

    static void ShowAge(int year, int month, int day)
    {
        var birthday = new LocalDate(year, month, day);
        // For consistency for future readers :)
        var today = new LocalDate(2012, 2, 3);

        Period period = Period.Between(birthday, today, YearMonth);
        Console.WriteLine("Birthday: {0}; Age: {1} years, {2} months",
                          birthday, period.Years, period.Months);
    }
}

只需要.NET的
DateTime
支持就可以了,但基本上你必须自己做算术。几乎可以肯定的是,这一点并不那么清楚。并不是说我有偏见或什么:)

此方法不需要任何外部库:

private static string AgeInYearsMonths(DateTime? DateOfBirth)
{
    if (DateOfBirth == null) return "";
    if (DateOfBirth >= DateTime.Today)
        throw new ArgumentException("DateOfBirth cannot be in future!");

    DateTime d = DateOfBirth.Value;
    int monthCount = 0;
    while ((d = d.AddMonths(1)) <= DateTime.Today)
    {
        monthCount++;
    }
    return string.Format("{0}.{1}", monthCount / 12, monthCount % 12);
}
私有静态字符串ageInYearsMonth(日期时间?出生日期)
{
如果(DateOfBirth==null)返回“”;
如果(DateOfBirth>=DateTime.Today)
抛出新ArgumentException(“出生日期不能在将来!”);
DateTime d=出生日期.Value;
int monthCount=0;

虽然((d=d.AddMonths(1))你检查了TimeSpan结构了吗?@Ravi:你有没有想过这是否能准确地确定两个日期之间的月数和年数?没有,想过要建议NodaTime,但当“daemon”处于活动状态时,那么为什么要担心,你会想出可靠的答案重要的是,答案取决于你使用的本土化日历。考虑一下最近在萨摩亚西部的一天的损失。我们能假设这个主题是在这个时区出生的吗?Jordell:这不是日历异常,那是时区的异常。在那里有细微的差别。(答案当然取决于日历,例如,在希伯来日历中,答案是“10年12个月”是合理的,但那是另一回事。)你,一个偏见,关于日期时间?不,不…;)@安德烈·巴伯:你必须承认,这是一个很好的代码:)@安德烈·巴伯:修复了,谢谢。我有时会用sf.net而不是googlecode.com,因为那里是乔达时代:)太好了,谢谢!是的;代码让我想,“我必须检查一下这个东西”。谢谢。野田佳彦的日子看起来很有趣,但我希望只依靠.NET API。在许多情况下,这将被完全打破。例如,以2012年2月1日的生日为例,加上“今天”或者2012年3月1日……那是29天前的事,使用这段代码会有0个月的时间。@AndrewBarber:真的没有。它坏了。@JonSkeet说的是我应该说的!我在谈论一些不同的东西;它没有按照我的建议坏掉。@linkerro:我有-我已经给出了一个具体的例子说明为什么它坏掉了。基本上你已经知道了忽略了“60天”代表不同的月数这一事实,这取决于起点。(同样,像这样选择
Month
,你永远不会返回0,你可以返回12,我怀疑这是不是有意的。)是的,我在向朋友解释时发现了这一点。我还发现了ToString(最近我忘了c#没有自动铸造)
private static string AgeInYearsMonths(DateTime? DateOfBirth)
{
    if (DateOfBirth == null) return "";
    if (DateOfBirth >= DateTime.Today)
        throw new ArgumentException("DateOfBirth cannot be in future!");

    DateTime d = DateOfBirth.Value;
    int monthCount = 0;
    while ((d = d.AddMonths(1)) <= DateTime.Today)
    {
        monthCount++;
    }
    return string.Format("{0}.{1}", monthCount / 12, monthCount % 12);
}