I'm using moment.js to calculate the difference between two dates in Qualtrics. The code below has worked really well so far. The dates use a YY-mm-dd format, and at the end, I get the difference in days.
const duedate= Qualtrics.SurveyEngine.getEmbeddedData('duedate');
const daystodue = moment(duedate).diff(moment(new Date()), 'days', false);
Qualtrics.SurveyEngine.setEmbeddedData('daystill', daystodue );
The problem is when I change the format for the embedded data 'duedate'
from YY-mm-dd to ISO 8601 (e.g., 2021-10-19T20:30:48Z) this stops working.
I know I'd probably need to change the format for new Date()
to ISO as well, or re-format 'duedate'
back to YY-mm-dd, but I'm struggling to figure this out. This is what I tried:
const duedate= Qualtrics.SurveyEngine.getEmbeddedData('duedate');
const daystodue = moment(duedate).diff(moment(new Date().format("YYYY-MM-
DDTHH:mm:ssZ")), 'days', false);
Qualtrics.SurveyEngine.setEmbeddedData('daystill', daystodue );
I was basing myself on this article, but I'm a beginner in JavaScript and couldn't figure out how to make it work.
Any help at all is much appreciated!