Indeed GREP is thinking in the wrong direction. GREP is nothing more than a (very) fancy search-and-replace function.
That said: the functionality you are looking for – math calculations – can be done inside InDesign through a script! A script has access to the Text Variables in your current document (https://helpx.adobe.com/indesign/using/text-variables.html) and so it can change their contents. And JavaScript (my preferred InDesign scripting language) has built-in operators to retrieve the current date.
If you write the above down in a basic script, you’ll practically have what you ask for; but then the problem is when to run this script. After all, if you cannot remember to change a year value once a year, you’d also be prone to forgetting to run the script at least once a year (“at least once”, because once run, it’d set the variable to the correct value for the entire year to come).
It’s far easier if your friendly InDesign kept the variable up-to-date. And it can! Here, copy this rather short-but-sweet script into the Extendscript Toolkit Editor and save it as a new file “set_experience_variable.jsx” in a folder called “Startup Scripts” – not your regular “Scripts Panel” folder. In case this is new for you, see “Download the script” in https://creativepro.com/add-missing-options-to-the-book-menu.php for a practical step-by-step instruction. (But no need to download that script.)
//DESCRIPTION:Write date difference in variable
// A Jongware Script 12-Jul-2019
#targetengine "set_experience_variable"
app.addEventListener("afterOpen", check_and_set_variable, false);
function check_and_set_variable (event)
{
var theDoc = event.parent;
if (theDoc instanceof Document)
{
var tvar = theDoc.textVariables.item('experience');
if (tvar.isValid && tvar.variableType == VariableTypes.CUSTOM_TEXT_TYPE)
{
tvar.variableOptions.contents = String(new Date().getFullYear() - 1976)
}
}
}
For the script to work at start-up, you must quit and restart InDesign once you copied this into the Startup Scripts folder, but afterwards it should fire automatically. What it does is, every time you OPEN an existing document, it checks if there is a Text Variable called “experience” in that document. If it is, it double-checks if its type is “Custom Text” (so it can change the contents), and if that is the case, it immediately sets the contents string to your difference in years.
You can see it works correctly: 1. Create a new document (or open an existing one). 2. Create a new Text Variable of type “Custom Text” and name it “experience”. Set its contents to some random text such as “oooh so clever”. 3. Insert this text variable somewhere in an appropriate place in the document text. For now, it’ll read “I have ooh so clever years of experience”. 4. Save and close the document. 5. The magic happens when opening this document again – please do so now!
If you followed all the steps above correctly, the text will now magically read “I have 43 years of experience”. And it will automatically update each new year!