PDA

View Full Version : Math based on Grad year


wcisme
April 10th, 2012, 03:00 PM
Im stuck. I need to calculate how many years since a person graduated. The data only has the last 2 digits in it. Like '97,'02, etc. I tried several different ways to go about this, no success. Any ideas?

step
April 10th, 2012, 04:01 PM
Give this code a shot:


var s = Left(Field("Grad Year"));

var year = 0;
var diff = 0;
var plural = "";

if (Int(s) <= 12) {
year = "20" + s;
}
else {
year = "19" + s;
}

diff = 2012 - year;

plural = (diff > 1) ? "years" : "year";

return (diff > 1) ? "It's been " + (2012 - year) + " " + plural + " since your graduation." : "You have yet to graduate.";


If the year is less that '12, it assumes the year is in the 2000's. If it's greater than '12, it assumes the year is 1900's.

wcisme
April 12th, 2012, 10:35 AM
Works like a charm! Thanks so much. 1 more question. The client is requesting the year returned to be spelled out instead of numeric. Could that be added to this code? We have talked about an array, but that seems like the long way to get there. I love clients.

Dan Korn
April 12th, 2012, 11:07 AM
Works like a charm! Thanks so much. 1 more question. The client is requesting the year returned to be spelled out instead of numeric. Could that be added to this code? We have talked about an array, but that seems like the long way to get there. I love clients.
A search for "JavaScript number to words" on this forum turns up several hits, and the same search in Google turns up even more, including this:
http://www.mail-archive.com/fusionpr.../msg01619.html (http://www.mail-archive.com/fusionpro@lists.printable.com/msg01619.html)

Personally, I think the first link (http://javascript.about.com/library/bltoword.htm) there is the best, since it's just a pure function without any web browser client code like a lot of JavaScript examples have. You can just copy the entire code from the box there into your JavaScript globals, then you can call the toWords function in any rule.

For example, if you add the code from that page to your JavaScript globals, then the last line of step's example could do this:
return (diff > 1) ? "It's been " + toWords(GetYear(Today()) - year) + " " + plural + " since your graduation." : "You have yet to graduate.";

wcisme
April 12th, 2012, 01:33 PM
Thanks guys!