15,964,117 members
1.00/5 (1 vote)
See more:
how to calculate age by using calender tool
Posted

## Solution 1

OnSelectionChangedevent of calendar,

C#
private void calendar1_onselectionchanged(object sender,EventArgs e)
{
int age= (DateTime.Today.Year - calendar1.SelectedDate.Year) * 372 + (DateTime.Today.Month - calendar1.SelectedDate.Month) * 31 + (DateTime.Today.Day - calendar1.SelectedDate.Day)) / 372;
}

Arul Murugan 2022 22-Jul-22 9:15am
Why 372 is approched

## Solution 2

I have a code in VB.Net, you can use it.
VB
Private Sub deDOB_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles deDOB.EditValueChanged
Dim years As Long

Select Case Today.Month
Case Is < deDOB.DateTime.Month
years = DateDiff(DateInterval.Year, deDOB.DateTime, Today) - 1

Case Is = deDOB.DateTime.Month
Select Case Today.Day
Case Is < deDOB.DateTime.Day
years = DateDiff(DateInterval.Year, deDOB.DateTime, Today) - 1
Case Is = deDOB.DateTime.Day
years = DateDiff(DateInterval.Year, deDOB.DateTime, Today)
Case Is > deDOB.DateTime.Day
years = DateDiff(DateInterval.Year, deDOB.DateTime, Today)
End Select
Case Is > deDOB.DateTime.Month
years = DateDiff(DateInterval.Year, deDOB.DateTime, Today)
End Select

txtAge.Text = years.ToString
End Sub