我在这里提供了一个输入信息但代码不起作用的场景。当我输入12/11/2015时,Address2Panel显示了什么。这是错误的,因为没有更多的日期可以输入,因为人A出生在2015年11月12日。该逻辑应输入过去5年的地址。但是,如果出生日期差距不比当前日期的5年时间更短,那就会出错。日期时间和公式改进C#
人生日= 2015年12月11日
某甲StartLiving = 2015年12月11日,因为它的一天,他/她就诞生了。 不应该显示Address2Panel
int CurrentDateInMonths = (((DateTime.Today.Year) * 12) + (DateTime.Today.Month));
static int AlienMonthsAtCurrentAddress = 0;
DateTime myDateTime;
//LivedHere = 12/11/2015
myDateTime = DateTime.Parse(LivedHere.Text);
AlienMonthsAtCurrentAddress = (CurrentDateInMonths - (((Convert.ToInt16(myDateTime.Year)) * 12) + Convert.ToInt16(myDateTime.Month)));
if (AlienMonthsAtCurrentAddress < 60)
{
Address2Panel.Visible = true;//shows the Address2Panel
}
else
{
ClearAddress2Panel();//hides also the Address2Panel
}
我应该如何提高我的公式和日期时间处理任何建议?
感谢您对DateTime操作的想法。但它不能解决我的问题。当人A出生日期= 12/11/2000,并且我把12/11/2015作为LivedHere地址。它没有显示我需要的额外字段。它应该显示出来,因为这个想法是输入过去5年的地址。和12/11/2015不是5年,当前日期是8/11/2017 – Ping
无论日期是什么,使用DateTime.Subtract恕我直言,离开!我编辑了我的答案 – Winnie