0

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!

  • If `duedate` is an ISO 8601 encoded string you should be able to make a JavaScript `Date` object from it (e.g. `new Date(duedate)`). Have you tried that? – Jason Oct 19 '21 at 21:02
  • When parsing strings with moment.js you should **always** supply the format [unless it's an ISO format](https://momentjs.com/docs/#/parsing/). – RobG Oct 19 '21 at 21:52

2 Answers2

0

moment.js is pretty smart to understand two different formats and make operations with that dates. I've tried this and it works:

const dueDate = '2021-10-19T20:30:48Z'
const firstDayOfYear = '2021-01-01'
moment(dueDate).diff(moment(firstDayOfYear), 'days', false)

returns 291

Note: I've used Moment.js v2.29.1

Gustavo Toro
  • 450
  • 4
  • 10
  • Thank you so much for this - I figured it out based on your insight! – Ilan Timerman Oct 19 '21 at 21:23
  • "I've tried this and it works" is not a good recommendation. By default, moment.js will parse ISO 8601 formatted timestamps. If any other format is supplied without a format string, it will be given to the built–in parser to parse (see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) about that). – RobG Oct 19 '21 at 21:54
  • Ok @RobG, thanks for the suggestion. I'll take it for my next answers. – Gustavo Toro Oct 19 '21 at 22:17
0

I ended up putting this together, which works:

const duedate= Qualtrics.SurveyEngine.getEmbeddedData('duedate');
const today= new Date( );
const daystodue= moment(duedate).diff(moment(today), 'days', false);
Qualtrics.SurveyEngine.setEmbeddedData('daystill', daystodue);

Thank you all for your insight!

  • Note that you don't need to do `const today = new Date(); moment(today);` you can just use `moment()` which creates a Moment object for the current date and time. – Heretic Monkey Oct 19 '21 at 21:26