Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
UPDATE: PLEASE SEE COMMENTS SECTION FOR CLARIFICATION. THX.

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>
public bool CriteriaResult(BsonDocument lead)
{
    //TODO: Insert Criteria Logic
    return true;
{
String dob = "dd/MM/yyyy";
DateTime dateOfBirth = DateTime.Parse( dob );
Int32 age = DateTime.Now.Year - dateOfBirth.Year;
}
if(Int32.TryParse(lead["Age"].ToString(), out age))
{
if(age <= 75 && age >= 45)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
Posted
Updated 29-Jul-13 14:18pm
v4
Comments
[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.

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:
 
Share this answer
 
Comments
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.
C#
DateTime now = DateTime.Today;
int age = now.Year - birthday.Year;
if (now < birthday.AddYears(age)) age--;

return age.ToString();


refer this link http://stackoverflow.com/questions/2194999/how-to-calculate-an-age-based-on-a-birthday[^]

then store the age in an array filter accordingly
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900