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 18:37pm
Edited 12-Dec-12 18:48pm
Jibesh16.6K
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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

public void GetAge(DateTime brithDate, out int days, out int months, out int years)
        {
            
            TimeSpan t =DateTime.Now.Subtract(brithDate);
            DateTime v = new DateTime(1, 1, 1);
            DateTime vv = v.AddDays(t.TotalDays);
            
            days = vv.Day - 1;
            months = vv.Month - 1;
            years = vv.Year - 1;
 

        }
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 21 Sep 2014
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