【问题标题】:Calculate days remaining to a birthday?计算生日的剩余天数?
【发布时间】:2010-11-13 07:18:39
【问题描述】:

我有一个带有某人生日的 DateTime 对象。我使用这个人的出生年月日创建了这个对象,方法如下:

DateTime date = new DateTime(year, month, day);

我想知道距离此人的下一个生日还有多少天。在 C# 中这样做的最佳方法是什么(我是该语言的新手)?

【问题讨论】:

  • @Mitch - 听起来是这样,但全文搜索在几秒钟内没有找到骗子,所以我想我会用清晰的方式写下这个问题......
  • @Russ - 抱歉,这是一个不同的问题。有一些相似之处,但肯定不一样(最好的证明是答案不同......)

标签: c# datetime


【解决方案1】:
// birthday is a DateTime containing the birthday

DateTime today = DateTime.Today;
DateTime next = new DateTime(today.Year,birthday.Month,birthday.Day);

if (next < today)
    next = next.AddYears(1);

int numDays = (next - today).Days;

如果生日是 2 月 29 日,这个简单的算法就会失败。这是替代方案(这与 Seb Nilsson 的答案基本相同:

DateTime today = DateTime.Today;
DateTime next = birthday.AddYears(today.Year - birthday.Year);

if (next < today)
    next = next.AddYears(1);

int numDays = (next - today).Days;

【讨论】:

  • +1 这与我现在使用的“手动”方式很接近,我想也许有一个巧妙的 C# 技巧可以简化它。
  • +1,我以为他使用了 DateTime.Now,其中包括当前时间。
  • 如果生日是 2 月 29 日并且当前年份不是闰年,这将失败
  • 这很好,很优雅,但是 2 月 29 日的处理有一个额外的边缘情况被遗漏了。考虑以下条件:生日是 2 月 29 日,并且已经过去,今年不是闰年,但下一年是。使用上面的当前逻辑,计算出的“天数”将比应有的天数少一,因为即使是闰年,您也会将 1 年添加到 2 月 28 日。请参阅我的答案以了解细微的单行调整。
【解决方案2】:

试试这个方法

private int GetDaysBeforeBirthday(DateTime birthdate)
{
    DateTime nextBday = new DateTime(DateTime.Now.Year, birthdate.Month, birthdate.Day);
    if (DateTime.Today > nextBday)
        nextBday = nextBday.AddYears(1);
    return (nextBday - DateTime.Today).Days;
}

只要过了你的生日,它就会返回你下一个生日前的剩余天数

【讨论】:

  • 这与 Philippe 的回答有何不同?
【解决方案3】:

使用今天的年份和生日的月份和日期不适用于闰年

经过一些测试,我开始工作了:

private static int GetDaysUntilBirthday(DateTime birthday) {
    var nextBirthday = birthday.AddYears(DateTime.Today.Year - birthday.Year);
    if(nextBirthday < DateTime.Today) {
        nextBirthday = nextBirthday.AddYears(1);
    }
    return (nextBirthday - DateTime.Today).Days;
}

在闰年的 2 月 29 日以及生日是同一天进行测试。

【讨论】:

  • 这是唯一可以 100% 工作的方法。原因是它使用了AddYears,它在二月的最后一个日期永远不会失败。
【解决方案4】:
        DateTime Variable = DateTime.Now;
        int NumOfDaysTillNextMonth = 0;
        while (Variable < Comparer) //Comparer is just a target datetime
        {
            Variable = Variable.AddDays(1);
            NumOfDaysTillNextMonth++;
        }

刚刚为一个程序必须这样做。如果您只需要剩余天数,那么与其他方法相比,它就足够简单了。

【讨论】:

    【解决方案5】:

    这是基于 Philippe Leybaert 上面的回答,但处理了一个额外的边缘情况,我认为在之前的任何答案中都没有考虑到这一情况。

    我要解决的极端情况是生日在闰日,今年的生日在过去,当年不是闰年,但下一年是。

    当前提供的答案将减少一天,因为它将“下一个”设置为当年的 2 月 28 日,然后添加一年,使日期为闰年的 2 月 28 日(这是不正确的)。更改一行可以处理这种极端情况。

    DateTime today = DateTime.Today;
    DateTime next = birthday.AddYears(today.Year - birthday.Year);
    
    if (next < today)
    {
        if (!DateTime.IsLeapYear(next.Year + 1))
            next = next.AddYears(1);
        else
            next = new DateTime(next.Year + 1, birthday.Month, birthday.Day);
    }
    
    int numDays = (next - today).Days;
    

    更新:根据 Philippe 指出我的代码存在相当大的缺陷进行了编辑。

    【讨论】:

    • 当今天的年份是除闰年之前的一年和生日为 2 月 29 日之外的任何年份时,此代码会崩溃。
    • 这应该(现在)被接受,因为它可以正确处理包括闰年在内的所有日期
    【解决方案6】:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DateTime dt1 = DateTime.Parse("09/08/2012");
                DateTime dt2 = DateTime.Parse(DateTime.Now.ToString());
                int days = (dt2 - dt1).Days;
                Console.WriteLine(days);
    
                double month = (dt2 - dt1).Days / 30;
                Console.WriteLine(month);
                double year = (dt2 - dt1).Days / 365;
                Console.WriteLine(year);
                Console.Read();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-11
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 2014-11-26
      相关资源
      最近更新 更多