How to do math operation in piped text for difference between two dates? | XM Community
Skip to main content
Solved

How to do math operation in piped text for difference between two dates?

  • May 15, 2021
  • 4 replies
  • 387 views

Sathwik_Teja
Forum|alt.badge.img

I am having 2 embedded fields called Hire Date and Termination date and I wanted to calculate tenure through piped text. How to insert a piped text for this. Hire date and Termination date are in date formats where as the tenure should be in a number format like for example 10 years they are in the service.

Best answer by Mishraji

You should be using JS for doing this. Following JS should work if difference in days is needed:

var hireDate = new Date("${q://QID1/ChoiceTextEntryValue/1}").getTime();
var terminationDate = new Date("${q://QID2/ChoiceTextEntryValue/1}").getTime();

    var diff = parseInt(terminationDate - hireDate);
    var days = diff/1000/60/60/24;
    Qualtrics.SurveyEngine.setEmbeddedData("Tenure", days);

4 replies

Mishraji
Level 4 ●●●●
Forum|alt.badge.img+18
  • Level 4 ●●●●
  • 236 replies
  • Answer
  • May 16, 2021

You should be using JS for doing this. Following JS should work if difference in days is needed:

var hireDate = new Date("${q://QID1/ChoiceTextEntryValue/1}").getTime();
var terminationDate = new Date("${q://QID2/ChoiceTextEntryValue/1}").getTime();

    var diff = parseInt(terminationDate - hireDate);
    var days = diff/1000/60/60/24;
    Qualtrics.SurveyEngine.setEmbeddedData("Tenure", days);


Forum|alt.badge.img+11
  • Level 4 ●●●●
  • 367 replies
  • October 13, 2021

Hi Mishraji,
Thanks for your code... I'm looking to do something similar but each day update an embedded field if another embeddedfield (eg. departure date) is within 1 day of today. If so, I'd update an embedded field (eg. Days to go) and then use an update ticket task and a ticket workflow scheduled email to send out a list to the relevent team.
Is there a way I could implement this code or something similar? My main problem is how you'd run through all responses and update those required. The new calulate metric task doesn't seem to handle dates and only works on NPS or averages. Also, creating a Formula embedded field in the data & analysis section also doesn't seem to handle dates either.

Any thoughts most welcome

Thanks

Rod Pestell


Henri from ilem
Level 1 ●
Forum|alt.badge.img+2
  • Level 1 ●
  • 10 replies
  • February 1, 2024

Hello,

This JS is perfect for US date formats (MMDDYYY). Has someone adapted it for the European date format DDMMYYY ? This would help. Thanks


Henri from ilem
Level 1 ●
Forum|alt.badge.img+2

By the way, Qualtrics added a new task “Data formula” in the workflows which solves this problem without the need of Java script. Very handy indeed. Thanks Qualtrics Product Management for listening to users requests.