Dynamic date fields in the knowledge

Hi Team,

One of my clients wants to add in a field for “Years of Experience” and wants to make this dynamic so that when they add in a “agents start date” field, they can have a separate field for “Years of Experience” update automatically throughout the year based on how long they have been at the company. Is this functionality currently possible or a future addition?

Best,
Davis

Hi Davis,

Great question and idea. It’s not possible to automatically pipe this into KG today but I was wondering if you could elaborate on what product you’re hoping to use this field for – Pages? Answers?

Thanks,
Alyssa

Alyssa,

This would be for Answers and the idea from what I am aware of is similar to an Excel formula or smartsheet that updates the data automatically.

Davis

Hi Davis,

Gotcha! That’s helpful context. If you’re looking to print this in Answers frontend, what I would recommend is using Javascript in your Answers component.js file for the Card.

For example, as a step one, you can use this Javascript function to get today’s current date/year like so:

let currentDate = new Date();
let currentYear = currentDate.getFullYear();

You can add these to your card under dataForRender.
Next, take a look at how the date is rendered in the LiveAPI - it may come out as a string like so:
"c_dateStarted": "2021-07-27",

In order to subtract the dateStarted from the currentYear, you’ll want to pull out the year as an integer rather than as a String. You can use parseInt() for this: parseInt(profile.c_dateStarted). Read more here: parseInt() - JavaScript | MDN

Depending on where you want to render this in your Answers card, you may want to add a new field such as “Experience” to the component.js and hbs files. Or you may want to add it to an existing field. such as Subtitle:

subtitle: parseInt(profile.c_dateStarted) - currentYear

This is just one potential method to accomplish this. Feel free to play around with the Javascript to tweak it to your needs.

Best,
Alyssa

Thank you for your help with this Alyssa! I will follow-up in the future if I am still having issues, but this was a super helpful walkthrough on how I could make this work for the client!

Davis