Skip to main content

I'm trying to calculate the age of a person returned in a search from their date of birth by creating a calcuated field and have run into some trouble.

 

If i create a calculated field titled "Age" with the forumula

 

DATEDIFF('day', [BirthDate], TODAY()) / 365

 

I get close, but obviously due to leap years this goes slightly wrong.

 

What I'm ideally trying to do is replicate the C# code I've written below

 

DateTime now = DateTime.Today;

int age = now.Year - DateOfBirth.Year;

if (DateOfBirth.Value.Date > now.AddYears(-age)) age--;

 

Return age;

 

I'm having trouble doing this in the calculated field dialog. If I do the below:

 

Create two parameters, year and day

 

write the code

Year = DATEDIFF('year', [BirthDate], TODAY())

Day = DATEDIFF('day', [BirthDate], TODAY())

 

I get a syntax error on my second line. I've searched but can't appear to find any requirement for an endline character. Clearly I'm missing something here but I'm puzzled as to what. Any help would be apreciated.

6 answers
0/9000