0

我有三个文本框,,,StartTextboxEndTextBoxTextbox3其中Textbox3包含No of Month。现在我想检查开始日期和结束日期的差异No of Month

这是自定义验证功能:

protected void ValidateDuration(object sender, ServerValidateEventArgs e)
{
    DateTime start = DateTime.Parse(StartTextBox.Text);
    DateTime end = DateTime.Parse(EndTextBox.Text);

    int months = (end.Month - start.Month) + 12 * (end.Year - start.Year);

    e.IsValid = months <= TextBox3;
}
4

2 回答 2

0

You can get the number of months between two date by using the overridden operation - to get a time span between 2 dates:

    DateTime date1 = new DateTime();
    DateTime date2 = new DateTime();

    date1 = DateTime.Now;
    date2 = DateTime.Now.AddMonths(4);

    TimeSpan months = date2 - date1;

   Console.WriteLine(months.Days / 30);

This code snippet will display 4 months

for your code:

e.IsValid = months <= Int32.TryParse(TextBox3.Text);
于 2012-04-29T06:18:53.960 回答
0

如果要检查months(an int) 的值TextBox3,则必须首先将值作为 astring并将其转换为 a int。例如:

protected void ValidateDuration(object sender, ServerValidateEventArgs e)
{
    DateTime start = DateTime.Parse(StartTextBox.Text);
    DateTime end = DateTime.Parse(EndTextBox.Text);

    int months = (end.Month - start.Month) + 12 * (end.Year - start.Year);
    int noOfMonth = int.Parse(TextBox3.Text);

    e.IsValid = months <= noOfMonth;
}
于 2012-04-29T06:02:56.390 回答