日期时间选择器验证

时间:2012-04-24 08:06:41

标签: c# winforms

我有两个datetimepickers(DateofBirth(DOB)和DateofIssue(DOI)),我需要验证这些日期时间选择器,DateofBirth(DOB)和DateOfIssue(DOI)之间应该有18年的差异。这该怎么做。请建议我的想法

3 个答案:

答案 0 :(得分:2)

DateTime birth;
DateTime issue;
if (birth.AddYears(18) > issue) {
    throw new Exception("Not 18 years between birth and issue");
}

答案 1 :(得分:0)

System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);

// diff1 gets 185 days, 14 hours, and 47 minutes.
int diff1 = date2.Subtract(date1).TotalYears;

答案 2 :(得分:0)