日期时间和公式改进C#

时间:2023-02-06 15:42:36

I got here a scenario where I input an information but the code does does not work. What happen is when I input 12/11/2015, the Address2Panel shows. Which is wrong because there no more date that I can input because Person A is born in 12/11/2015. The logic should Enter addresses for the past 5 years. But it goes wrong if the Birthdate gap is not lesser the 5 years from the current date.

我在这里输入了一个信息,但是代码确实没有用。当我输入12/11/2015时,会发生什么,Address2Panel显示。这是错的,因为没有更多的日期我可以输入,因为人A出生于2015年11月12日。逻辑应输入过去5年的地址。但如果出生日期差距在当前日期的5年内不小,那就错了。

Person A Birthday = 12/11/2015

人生日= 12/11/2015

Person A StartLiving = 12/11/2015 because its the day he/she was born. Should not display Address2Panel

人A StartLiving = 12/11/2015因为他/她出生的那一天。不应该显示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
        }

Any suggestion how should I improve my formula and date time manipulation?

有什么建议我应该如何改进我的公式和日期时间操作?

2 个解决方案

#1


0  

no need to convert date into months, subtract dates using DateTime.Subtract method :
From MSDN, DateTime.Subtract Method subtracts the specified date and time from this instance.It returns a TimeSpan object which has a property Days

无需将日期转换为月份,使用DateTime.Subtract方法减去日期:从MSDN,DateTime.Subtract方法从此实例中减去指定的日期和时间。它返回具有属性Days的TimeSpan对象

static int AlienMonthsAtCurrentAddress = 0;
try
{

    DateTime myDateTime;
    myDateTime = DateTime.Parse(LivedHere.Text);

    // If you don't wish to subtract from today's date use required date in place of DateTime.Now
    TimeSpan span = DateTime.Now.Subtract ( myDateTime ); 
    if (span.Days < 60)
    {
        Address2Panel.Visible = true;//shows the Address2Panel
    }
    else
    {
        ClearAddress2Panel();//hides also the Address2Panel
    }

}
catch { }

#2


0  

You could check years, months and days with a separately logic, and then get them all together:

您可以使用单独的逻辑检查年,月和日,然后将它们全部组合在一起:

DateTime date = new DateTime(2015,11,12)
DateTime input = getDate()
int years = input.Year - date.Year - 1
years += If(input.Month > date.Month, 1, 0)
years += If(input.Month = date.Month AndAlso input.Day >= date.Day, 1, 0)

This will output the exact number of years between two days (truncating the resulting integer). You'll just have to compare it to 5, in your case

这将输出两天之间的确切年数(截断结果整数)。在你的情况下,你只需要将它与5进行比较

#1


0  

no need to convert date into months, subtract dates using DateTime.Subtract method :
From MSDN, DateTime.Subtract Method subtracts the specified date and time from this instance.It returns a TimeSpan object which has a property Days

无需将日期转换为月份,使用DateTime.Subtract方法减去日期:从MSDN,DateTime.Subtract方法从此实例中减去指定的日期和时间。它返回具有属性Days的TimeSpan对象

static int AlienMonthsAtCurrentAddress = 0;
try
{

    DateTime myDateTime;
    myDateTime = DateTime.Parse(LivedHere.Text);

    // If you don't wish to subtract from today's date use required date in place of DateTime.Now
    TimeSpan span = DateTime.Now.Subtract ( myDateTime ); 
    if (span.Days < 60)
    {
        Address2Panel.Visible = true;//shows the Address2Panel
    }
    else
    {
        ClearAddress2Panel();//hides also the Address2Panel
    }

}
catch { }

#2


0  

You could check years, months and days with a separately logic, and then get them all together:

您可以使用单独的逻辑检查年,月和日,然后将它们全部组合在一起:

DateTime date = new DateTime(2015,11,12)
DateTime input = getDate()
int years = input.Year - date.Year - 1
years += If(input.Month > date.Month, 1, 0)
years += If(input.Month = date.Month AndAlso input.Day >= date.Day, 1, 0)

This will output the exact number of years between two days (truncating the resulting integer). You'll just have to compare it to 5, in your case

这将输出两天之间的确切年数(截断结果整数)。在你的情况下,你只需要将它与5进行比较