Click here to Skip to main content
12,503,681 members (54,913 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
hi sir

I Want to say that i am creating a registration form to work properly.
i did code for get age but it doesn't work properly .
i want year, month day but my code is only show year how will get year month & days
this is my code
private void button1_Click(object sender, EventArgs e)
{
   DateTime dob = Dob_dateTimePicker.Value;
   DateTime PresentYear = Current_dateTime.Value;
            
 
   TimeSpan ts = PresentYear - dob;
   int Age = ts.Days / 365;
 
   textBox1.Text = Age.ToString() ;+//" Year" + "Month" +"Days";
           
} 

Please Explain Me How To Get Age = Year ,Month & Days

Regards
Umashankar sahu
Posted 12-Dec-12 17:37pm
Updated 23-May-16 22:01pm
Jibesh17.4K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

DateTime dob = Convert.ToDateTime("18 Feb 1987");
            DateTime PresentYear = DateTime.Now;
            TimeSpan ts = PresentYear - dob;
            DateTime Age = DateTime.MinValue.AddDays(ts.Days);
            MessageBox.Show(string.Format(" {0} Years {1} Month {2} Days", Age.Year - 1, Age.Month - 1, Age.Day - 1));
  Permalink  
Comments
ProgramFOX 16-Dec-12 11:56am
   
+5!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Go through this. you have the complete code for age calculation.
http://raasukutty.wordpress.com/2009/06/18/c-calculate-age-in-years-month-and-days/[^]
  Permalink  
Comments
ProgramFOX 16-Dec-12 11:56am
   
+5!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hi,
DateTime age = new DateTime(PresentYear.Subtract(dob).Ticks);
Should do the job. If you want to output a readable format,
textBox1.Text = age.Year.ToString() + " years " + age.Month.ToString() + " months " + age.Day.ToString() + " days ";
should be OK.

Regards.
  Permalink  
Comments
CHill60 12-Oct-15 9:56am
   
Shows incorrect age.

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
Web01 | 2.8.160919.1 | Last Updated 10 Jun 2016
Copyright © CodeProject, 1999-2016
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