1

我有一些获取日期的代码:

 DateTime startDate = new DateTime(2010, 1,4);

我想创建一个函数

 string message = CalculateUpcomingAnniversary(startDate);

我正在计算今天是否可能是即将到来的周年纪念日

因此,如果接下来 5 天即将到来周年纪念日(鉴于此开始日期),我希望有一个函数返回类似“3 周年纪念日即将在 4 天内......”

对于其他任何事情,我只想返回空字符串。计算这个最有效的方法是什么?

因此,鉴于上面的这个开始日期,如果我今天运行它会返回一个空白字符串,但如果我在 2014 年 1 月 5 日运行这个函数,它将返回一个字符串“ 1 天 4 周年

4

4 回答 4

5

您可能希望将其拆分为两个功能。首先像这样找到下一个周年纪念日:

public DateTime GetNextAnniversary(DateTime startDate)
{
    var now = DateTime.Now
    var next = startDate.AddYears(now.Year - startDate.Year);
    return now > next ? next.AddYears(1) : next;
}

您可以像这样格式化日期:

public string GetAnniversaryString(DateTime startDate)
{
    var next = GetNextAnniversary(startDate);
    int nYears = next.Year - startDate.Year;
    var span = next - DateTime.Now;
    return span.Days <= 5 
        ? string.Format("{0} year anniversary in {1} days", nYears, span.Days)
        : string.Empty;
}

或者更完整的方法可能看起来像这样:

var now = DateTime.Now;
var next = GetNextAnniversary(startDate);
int nYears = next.Year - startDate.Year;
var span = next - DateTime.Now;
if (span.Days <= 30)
{
    return string.Format("{0} year anniversary in {1} days", nYears, span.Days);
}
else
{
    int months = next.Month - startDate.Month;
    if (next.Year > now.Year)
    {
        months += CultureInfo.CurrentCulture.Calendar.GetMonthsInYear(now.Year);
    }

    return string.Format("{0} year anniversary in {1} months", nYears, months);
}

当然,这是一个相当粗糙的字符串格式化函数,并且可以显着改进。

于 2013-05-09T02:04:52.863 回答
0
 protected void Page_Load(object sender, EventArgs e)
        {

            GetAnniversarydate();   


        }

 protected void GetAnniversarydate()
        { 
         DateTime _anniversarydate = new DateTime(2005, 6, 9);

            DateTime _currentdate = Convert.ToDateTime(DateTime.Now.ToShortDateString());

            //Comparing the anniversary day and month with current day and month
            if ((_anniversarydate.Day == _currentdate.Day) && (_anniversarydate.Month == _currentdate.Month))
            {
                int _Yeardifference = _currentdate.Year - _anniversarydate.Year;

                Response.Write(string.Format("Today is your {0} anniversary",_Yeardifference));

            }
        }
于 2013-05-09T05:02:31.297 回答
0

我会建议如下内容(根据要求,它会检查 <5 天的提前通知):

DateTime _now = DateTime.Now;

TimeSpan _ts = _now - startDate;

// days till anniversary
if(_ts.Days>5) return null;

// next anniversary, intermediate result
DateTime _nextAnniversary = new DateTime(_now.Year, startDate.Month, startDate.Day);

// next anniversary, final result
return (_now > _nextAnniversary )? _nextAnniversary.AddYears(1):_nextAnniversary;

这段代码片段可以封装成完整的函数,它可以同时返回 _ts.Days_nextAnniversary值(然后你应该定义一些要返回的结构或对象),或者只是_nextAnniversary,因为可以很容易地计算出未来的日子,如代码所示(_ts.Days

于 2013-05-09T02:24:35.600 回答
-1

这听起来像一个有趣的问题,所以我解决了它。我还处理了闰年与天数的混淆,这对其进行了测试以证明这一点。干得好:

编辑:固定和简化。

public class AnniversaryConcept : IConceptable
{
    public const int DAYS_TO_LOOK_FORWARD = 5;

    public void Run()
    {
        Console.WriteLine(AnniversaryMessage(GetPastDate(6, -1)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(5, -1)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(4, -1)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(3, -1)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(2, -1)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(1, -1)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(0, -1)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(-1, -1)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(-2, -1)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(-3, -1)));

        Console.WriteLine(AnniversaryMessage(GetPastDate(2, -1)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(2, -2)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(2, -3)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(2, -4)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(2, -5)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(2, -6)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(2, -7)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(2, -8)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(2, -9)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(2, -10)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(2, -11)));
        Console.WriteLine(AnniversaryMessage(GetPastDate(2, -12)));
    }

    private DateTime GetPastDate(int daysOffset, int yearsOffset)
    {
        return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddYears(yearsOffset).AddDays(daysOffset);
    }

public string AnniversaryMessage(DateTime originalDate)
{
    if (originalDate > DateTime.Now)
    {
        return null;
    }

    int dayDifference = (new DateTime(DateTime.Now.Year, originalDate.Month, originalDate.Day).Date
                                    - DateTime.Now.Date).Days;

    // Already past this date.
    if (dayDifference < 0 || dayDifference > DAYS_TO_LOOK_FORWARD)
    {
        return null;
    }

    return string.Format("{0} year anniversary in {1} days.", 
        DateTime.Now.Year - originalDate.Year, 
        dayDifference);
}
}
于 2013-05-09T02:45:14.677 回答