计算两个日期之间的差异并获得以年为单位的值?

发布于 2024-09-07 21:34:50 字数 359 浏览 2 评论 0原文

可能的重复:
如何在 C# 中计算某人的年龄?

我想基本上计算员工的年龄 - 所以我们有每个员工的出生日期,依此类推 在 C# 方面,我想做这样的事情 -

int age=Convert.Int32(DateTime.Now-DOB);

我可以使用天数并进行操作,然后获取年龄...但我想知道是否有一些东西可以直接使用来获取年数。

Possible Duplicate:
How do I calculate someone’s age in C#?

I want to calculate basically the age of employees - So we have DOB for each employee, So on
the C# Side I want to do something like this -

int age=Convert.Int32(DateTime.Now-DOB);

I can use days and manipulate then get the age...but I wanted to know if there something I can use directly to get the number of years.

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

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

发布评论

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

评论(6

挽手叙旧 2024-09-14 21:34:50

您想计算员工的年龄吗?然后您可以使用此代码段(来自 Calculate Age in C# ):

DateTime now = DateTime.Today;
int age = now.Year - bday.Year;
if (bday > now.AddYears(-age)) age--;

如果没有,请具体说明。我很难理解你想要什么。

Do you want calculate the age in years for an employee? Then you can use this snippet (from Calculate age in C#):

DateTime now = DateTime.Today;
int age = now.Year - bday.Year;
if (bday > now.AddYears(-age)) age--;

If not, then please specify. I'm having a hard time understanding what you want.

乖乖公主 2024-09-14 21:34:50

减去两个 DateTime 得到一个 TimeSpan 回来。不幸的是,它返回的最大单位是天。

虽然不准确,但您可以估计它,如下所示:

int days = (DateTime.Today - DOB).Days;

//assume 365.25 days per year
decimal years = days / 365.25m;

编辑:哎呀,TotalDays 是一个双精度数,Days 是一个 int。

Subtracting two DateTime gives you a TimeSpan back. Unfortunately, the largest unit it gives you back is Days.

While not exact, you can estimate it, like this:

int days = (DateTime.Today - DOB).Days;

//assume 365.25 days per year
decimal years = days / 365.25m;

Edit: Whoops, TotalDays is a double, Days is an int.

看轻我的陪伴 2024-09-14 21:34:50

网站上,他们有:

   public static int CalculateAge(DateTime BirthDate)
   {
        int YearsPassed = DateTime.Now.Year - BirthDate.Year;
        // Are we before the birth date this year? If so subtract one year from the mix
        if (DateTime.Now.Month < BirthDate.Month || (DateTime.Now.Month == BirthDate.Month && DateTime.Now.Day < BirthDate.Day))
        {
            YearsPassed--;
        }
        return YearsPassed;
  }

On this site they have:

   public static int CalculateAge(DateTime BirthDate)
   {
        int YearsPassed = DateTime.Now.Year - BirthDate.Year;
        // Are we before the birth date this year? If so subtract one year from the mix
        if (DateTime.Now.Month < BirthDate.Month || (DateTime.Now.Month == BirthDate.Month && DateTime.Now.Day < BirthDate.Day))
        {
            YearsPassed--;
        }
        return YearsPassed;
  }
雪若未夕 2024-09-14 21:34:50
    private static Int32 CalculateAge(DateTime DOB)
    {
        DateTime temp = DOB;
        Int32 age = 0;
        while ((temp = temp.AddYears(1)) < DateTime.Now)
            age++;
        return age;
    }
    private static Int32 CalculateAge(DateTime DOB)
    {
        DateTime temp = DOB;
        Int32 age = 0;
        while ((temp = temp.AddYears(1)) < DateTime.Now)
            age++;
        return age;
    }
浊酒尽余欢 2024-09-14 21:34:50

Math.Round(DateTime.Now.Subtract(DOB).TotalDays/365.0)

正如所指出的,这是行不通的。您必须这样做:

(Int32)Math.Round((span.TotalDays - (span.TotalDays % 365.0)) / 365.0);

此时另一个解决方案不太复杂,并且在更大的跨度上仍然准确。

编辑2,怎么样:

Math.Floor(DateTime.Now.Subtract(DOB).TotalDays/365.0)

天哪,我这些天基础数学很糟糕......

Math.Round(DateTime.Now.Subtract(DOB).TotalDays/365.0)

As pointed out, this won't work. You'd have to do this:

(Int32)Math.Round((span.TotalDays - (span.TotalDays % 365.0)) / 365.0);

and at that point the other solution is less complex and continues to be accurate over larger spans.

Edit 2, how about:

Math.Floor(DateTime.Now.Subtract(DOB).TotalDays/365.0)

Christ I suck at basic math these days...

烦人精 2024-09-14 21:34:50
(DateTime.Now - DOB).TotalDays/365

从另一个 DateTime 结构中减去 DateTime 结构将得到一个具有 TotalDays 属性的 TimeSpan 结构...然后除以 365

(DateTime.Now - DOB).TotalDays/365

Subtracting a DateTime struct from another DateTime struct will give you a TimeSpan struct which has the property TotalDays... then just divide by 365

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