?
Solved

Calculate age from date of birth

Posted on 2010-01-05
4
Medium Priority
?
2,257 Views
Last Modified: 2012-05-08
Hi
Just asked this question before and answered points but realised I needed another solution.

I want to calculate a persons age by passing in the date of birth - however the date of birth is a nullable type.              DateTime?

I am using linq to sql and want a method to calculate the age taking into account the nullable date of birth.

So something like

Age = db.CalculatePersonsAge(DOB)

Age is an integer and DOB is nullable
0
Comment
Question by:mugsey
  • 3
4 Comments
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26179915

DateTime? birthDate = null;
            
            birthDate = new DateTime(1965, 5, 1);
            var age = (birthDate == null) ? -1: DateTime.Now.AddYears(-1*((DateTime)birthDate).Year).Year;

Open in new window

0
 
LVL 13

Accepted Solution

by:
zadeveloper earned 2000 total points
ID: 26179928
Just replace -1 with required age if dob is null
public int CalculatePersonsAge (DateTime? dob)
        {
            return (dob == null) ? -1 : DateTime.Now.AddYears(-1 * ((DateTime)dob).Year).Year;
        }

Open in new window

0
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26179996
Is this working for you ?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 26180064
Small example
private void Form1_Load(object sender, EventArgs e)
        {
            DateTime? dob = null;
            int age;
            age = CalculateAge(dob);
            
            age = CalculateAge(new DateTime(1979,02,11));

        }

        private int CalculateAge(DateTime? dob)
        {
            if (dob == null)
                return -1;

            

            int years = DateTime.Today.Year - ((DateTime)dob).Year;
            int months = DateTime.Today.Month - ((DateTime)dob).Month;
            int days = DateTime.Today.Day - ((DateTime)dob).Day;

            DateTime birthDayThisYear = new DateTime(System.DateTime.Now.Year,((DateTime)dob).Month,((DateTime)dob).Day);

			if(birthDayThisYear > DateTime.Today)//you've not yet celebrated your birthday this year
			{
				years -= 1;
				months += 12;
			}
			if(birthDayThisYear.Day > DateTime.Today.Day)
			{
				months -= 1;
			}
            return years;
        }

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question