15,963,836 members
1.00/5 (1 vote)
See more:

Hi, I'm trying to set up a function that calculates age from dob (dd/mm/yyyy) then takes the resulting age and determines if it's true/false within a range of minimum to maximum. I've researched the following function online and so far have come up with the following. Would anyone please help me out?

C#
```/// <summary>
/// Function will be ran during criteria check
/// Must return a true or false value
/// Resulting function must be called CriteriaResult
/// </summary>
/// <returns>True False Result</returns>
{
//TODO: Insert Criteria Logic
return true;
{
String dob = "dd/MM/yyyy";
DateTime dateOfBirth = DateTime.Parse( dob );
Int32 age = DateTime.Now.Year - dateOfBirth.Year;
}
{
if(age <= 75 && age >= 45)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}```
Posted
Updated 29-Jul-13 14:18pm
v4
[no name] 29-Jul-13 19:26pm
Help you out with what? Your code block is incomplete and you did not describe any kind of problem.
Fabianluna 29-Jul-13 20:07pm
#ThePhantomUpvoter, thanks for letting me know the block was incomplete, I have updated the post. The problem is I don't know the best way to approach this function since all I have is the dob in (dd/mm/yyyy) format and I need to first determine the age then determine if the age is within a range; the example above is my attempt to determine if a record is within the age of 45 and 75.
Garth J Lancaster 29-Jul-13 20:14pm
It depends what else you wish to do to/with date ...you've suggested you have two uses - I'd be thinking of a simple class, to encapsulate/store the date , with two methods - calculate_age, check_valid etc .. otherwise, you really have to have two seperate functions
[no name] 29-Jul-13 20:23pm
Thanks for the update. You still have not said what the problem is but this might give you a start
1. Your code will not compile. You have to have code segment inside methods, not outside.
2. "dd/MM/yyyy" will throw an exception when your code is run.
3. You calculate the age and throw it away by using the same variable to parse lead["Age"] so unless you are really try to figure out the lead["Age"]...
4. You have too many else statements.

## Solution 1

Refer this one
```protected void Button1_Click(object sender, EventArgs e)
{
DateTime Birth = new DateTime(1954, 7, 30);
DateTime Today = DateTime.Now;

TimeSpan Span = Today - Birth;

DateTime Age = DateTime.MinValue + Span;

// note: MinValue is 1/1/1 so we have to subtract...
int Years = Age.Year - 1;
int Months = Age.Month - 1;
int Days = Age.Day - 1;
Response.Write(Years.ToString() + " Years, " + Months.ToString() + " Months, " + Days.ToString() + " Days<br />");
}```

TimeSpan Years and Months[^]

Regards..:laugh:

Fabianluna 30-Jul-13 11:12am
This is exciting - thanks so much to all of you, I'll give this a shot today and will post my results.
Thanks7872 30-Jul-13 13:21pm
Sure.

## Solution 2

C#
```DateTime now = DateTime.Today;
int age = now.Year - birthday.Year;

return age.ToString();
```

then store the age in an array filter accordingly

v2