Click here to Skip to main content
11,719,185 members (87,624 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi All,

How to calculate age based on "date time picker selection" in windows application with c# coding.


Thanks and Regards,
Murali.
Posted 30-Dec-12 23:53pm
Edited 30-Dec-12 23:59pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

int myAge = 
DateTime.Today.Year - dateTimePicker1.Value.Year; // CurrentYear - YourBirthDate
 
 textBox1.Text = myAge.ToString();
  Permalink  
v3
Comments
ProgramFOX at 31-Dec-12 6:49am
   
I'm not sure about this. If 'Today' is 30 Dec 2012, and if your birth date is 31 Dec 2011, then your output will be '1'. But that's not correct, because '1' is only correct from 31 Dec 2012 up to and including 30 Dec 2013.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,

Have a look here:
Calculating Duration Between Two Dates in Years, Months and Days[^]
To get the value (DateTime) from DateTimePicker, try this:
DateTime dt = dateTimePicker1.Value;
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this..

TimeSpan ts=DateTime.Now - Convert.ToDateTime(datePicker.SelectedDate);
int age = Convert.ToInt32(ts.Days) / 365;

hope this will heip you.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Is there a whole small sample program on this? Thanks in advance...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim iMonths As Integer
Dim iYears As Integer
Dim iDays As Integer
iYears = Now.Year - DateTimePicker1.Value.Year
iMonths = Now.Month - DateTimePicker1.Value.Month
iDays = Now.Day - DateTimePicker1.Value.Day

If iMonths + 12 < 12 Then
iYears = Years - 1
iMonths = 12 + Months
iDays = 30 + Days
End If

TextBox1.Text = iYears & " Years " & iMonths & "Months" & iDays & "Days"
End Sub
  Permalink  
Comments
CHill60 at 21-Jul-15 6:28am
   
Firstly, this post was resolved over 2 years ago, and was tagged C# not VB so your response is "Off Topic"
Secondly, it won't even compile (iYears not Years, iMonths not Months and iDays not Days), so your response is "Unclear or Incomplete"
Lastly, your logic is just wrong - why add 30 days? So your post is "Inaccurate/Misleading"
I suggest you remove it to avoid down-votes and reports

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 1,734
1 OriginalGriff 690
2 Maciej Los 569
3 Richard MacCutchan 540
4 F-ES Sitecore 510


Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 16 Jul 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100