PDA

View Full Version : Saultation changed based on age

tschulte72
June 11th, 2013, 11:54 AM
I recently had a customer who asked for a letter that used an alternate salutation if the member was under 18.

ie.
Dear Parent or Gaurdian of John Smith, (if under 18)
or
Dear John Smith, (if over 18)

I came up with the following and it seems to work well so I thought I'd share it with anyone who might need something similar.

The fuction that calculates the age based on a date uses the format MM/DD/YYYY for the provided birthday.

function getAge(birth) {
var today = new Date();
var curr_date = today.getDate();
var curr_month = today.getMonth() + 1;
var curr_year = today.getFullYear();

var pieces = birth.split('/');
var birth_date = pieces[1];
var birth_month = pieces[0];
var birth_year = pieces[2];

if (curr_month == birth_month && curr_date >= birth_date) return parseInt(curr_year-birth_year);
if (curr_month == birth_month && curr_date < birth_date) return parseInt(curr_year-birth_year-1);
if (curr_month > birth_month) return parseInt(curr_year-birth_year);
if (curr_month < birth_month) return parseInt(curr_year-birth_year-1);
}
var age = getAge(Field("Birthday"));
return (age < 18) ? "Dear Parent or Guardian of " + Field("Name") + "," : "Dear " + Field("Name") + ",";