Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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
Edited 12-Dec-12 17:48pm
Jibesh16.5K
v2
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 at 16-Dec-12 11:56am
   
+5!
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 at 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  

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

  Print Answers RSS
0 OriginalGriff 310
1 Sergey Alexandrovich Kryukov 176
2 PhilLenoir 164
3 Richard MacCutchan 160
4 Sharmanuj 146
0 Sergey Alexandrovich Kryukov 6,081
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 16 Dec 2012
Copyright © CodeProject, 1999-2014
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