客户验证程序问题说明差异日期 [英] Customer Validator Problem Caluclate Difference Dates
本文介绍了客户验证程序问题说明差异日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个文本StartTextbox和EndTextBox,Textbox3.请小心textbox3(每月的月份数).现在,我希望开始日期和结束日期之间的差异是按月数检查的.
这在customvalidation中起作用:
I have three text StartTextbox and EndTextBox ,Textbox3 .Please careful textbox3(No of month). Now I want difference of start date and end date is Checked by No of Months .
Here function in customvalidation:
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;
}
推荐答案
你好
您无法使用此公式计算两个月之间的差额. (end
.Month
-start
.Month
)
想象end
是4/29/2012而start
是4/29/1995,结果将是0 :)
那你该怎么办?
看这个例子:
如果日子不重要
Hello
You can not calculate difference between 2 months with this formula. (end
.Month
-start
.Month
)
Imagineend
is 4/29/2012 andstart
is 4/29/1995, the result will be 0 :)
So, what could you do?
Look at this sample:
If the day is not important
DateTime start = DateTime.Parse("4-2-2011");
DateTime end = DateTime.Now;
DateTime s = start;
int amount = 0;
while (s.Year != end.Year || s.Month != end.Month)
{
s= s.AddMonths(1);
amount++; //difference between 2 months
}
但是:如果一天很重要,那么可以将其添加到代码中:
But: If the day is important then you can add this to the code:
int days = s.Day -end.Day ;
if (days < 0)
amount--;
为什么不使用TimeSpan
why don''t you use TimeSpan
TimeSpan ts = enddate - startdate;
int month = ts.Days / 30;
这篇关于客户验证程序问题说明差异日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文