问题
I have a problem with a Windows Form
application which has two DateTimePicker
controls showing "DATE OF BIRTH" and "DATE OF JOINING".
I want to compare the values of these controls such that date of birth should be not less than and not greater than and should not be equal less than and not greater than and should not be equal...
How can I do this?
回答1:
Try this code
DateTimePicker dtBDay = new DateTimePicker();
dtBDay.Value = DateTime.Now.AddYears(-5);
DateTimePicker dtJoin = new DateTimePicker();
dtJoin.Value = DateTime.Now;
if (dtBDay.Value >= dtJoin.Value)
{
throw new Exception("Date of Join cannot be less than or equal to Date of Birth");
}
Hope this will help
回答2:
As suggested by jbutler483
I tried to do but I couldn't get any solution finally as my own knowledge I used following code
private void dtpdob_ValueChanged(object sender, EventArgs e)
{
int year = DateTime.Today.Year - dtpdob.Value.Year;
int month = DateTime.Today.Month - dtpdob.Value.Month;
int day = DateTime.Today.Day - dtpdob.Value.Day;
var a = year.ToString();
var b = month.ToString();
var c = day.ToString();
if (day != 0 && month!=0 && year!=0)
{
if(day!=1)
txtage.Text = a + " Years " + b + " Months " + c + " Days ";
else
txtage.Text = a + " Years " + b + " Months " + c + " Day ";
}
else if (day == 0 && month == 0)
txtage.Text = a + " Years ";
else if (day == 0 && month != 0)
txtage.Text = a + " Years " + b + " Months ";
else
txtage.Text = a + " Years " + b + " Months " + c + " Days ";
}
来源:https://stackoverflow.com/questions/25663033/how-to-compare-two-datetimepicker-values-in-c-sharp