我有一些代码可以获取日期:
DateTime startDate = new DateTime(2010, 1,4);
我想创建一个函数
string message = CalculateUpcomingAnniversary(startDate);
我试图计算今天是否可能是即将到来的周年纪念日
因此,如果在接下来的5天内有一个周年纪念日(考虑到这个开始日期),我希望有一个功能可以返回“4天内即将到来的4天......”。
对于其他任何我只想返回空字符串。计算这个的最有效方法是什么?
所以考虑到上面这个开始日期,如果我今天运行它会返回一个空白字符串,但如果我在2014年1月5日运行此功能,它将返回一个字符串“ 4周年1天< /强>“
答案 0 :(得分: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);
}
当然这是一个相当粗略的字符串格式化功能,可以显着改进。
答案 1 :(得分:0)
我会建议以下内容(根据要求检查&lt; 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
)
答案 2 :(得分: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));
}
}
答案 3 :(得分:-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);
}
}