Click here to Skip to main content
12,695,447 members (27,276 online)
Rate this:
 
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 31-Dec-12 0:53am
Updated 31-Dec-12 0:59am
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 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 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
Jayabalaji V 5-Jul-16 8:02am
   
Hey i calculated date between 05/07/2016 and 16/01/2017.

In this days exceeds more than 30 days like "0 Years 6 Months 41 Days.

How to fix this.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 16 Jul 2015
Copyright © CodeProject, 1999-2017
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