Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate age in years from dob in c# [duplicate]

Tags:

date

c#

asp.net

   private void button1_Click(object sender, EventArgs e)
    {
        DateTime dob = new DateTime();
        textBox1.Text = dob.ToString();
        int age;
        age = Convert.ToInt32(textbox2.Text);
        age = DateTime.Now.Year - dob.Year;
        if (DateTime.Now.DayOfYear < dob.DayOfYear)
            age = age - 1;

    }

How to claculate the age from dob.This is my form1.cs.any ideas please

like image 573
Dilipan K Avatar asked Feb 08 '13 05:02

Dilipan K


People also ask

How do you calculate age in years?

Take the difference between the current day and their birth day: days = current day - birth day . Replace these differences in the age formula: age = (years × 365) + (months × 31) + days . This is the person's age in days. Divide the result by 365 to get the age in years.

How do you calculate age from birth year and year?

Ans: To find out a person's age, all you need is that person's year of birth. After this, all you need to do now is subtract the birth year from the ongoing current year and you will have the age. This will help you to calculate the age from the date of birth. Age= 2020- 1966 = 54.

How do you calculate age in C sharp?

int age = (int) ((DateTime. Now - bday). TotalDays/365.242199);


1 Answers

DateTime today = DateTime.Today;

int age = today.Year - bday.Year;

if (bday > today.AddYears(-age))
 age--;
like image 96
VasanthRavichandran Avatar answered Oct 14 '22 15:10

VasanthRavichandran