Solved
Advice on Moment.js and converting time to be Qualtrics readable
Hello!
Thanks for taking a look. I've tried valiantly to get some Javascript coding to work with Moment.js, but I'm struggling with my incompetency. This is what's going on: I'm receiving a unix timestamp in milliseconds, being passed through a query-string and recorded in a Qualtrics embedded variable. I'm trying to convert that timestamp into: YYYY-MM-DD, so that (hopefully), Qualtrics will read it and treat it as a date. To test it I've been sending in the UNIX: 1516985915307, which should convert to 2018-01-27. However, I get an Invalid Date, output in the embedded variable I'm trying to write the converted date to.
<code>Qualtrics.SurveyEngine.addOnReady(function()</code>
<code>{</code>
<code>var UNIX= Qualtrics.SurveyEngine.getEmbeddedData('UNIX');</code>
<code>var dateString = moment('UNIX').format("MM-DD-YYYY");</code>
<code>Qualtrics.SurveyEngine.setEmbeddedData('NEW_DATE', dateString);</code>
<code>});</code>
It does write to the UNIX embedded data variable, but again, "Invalid Date" for "NEW_DATE"
Any help or ideas would be greatly appreciated.
Thanks!
Michael
Best answer by Anonymous
Please use the following code:
var date = new Date(parseInt(Qualtrics.SurveyEngine.getEmbeddedData('UNIX')));
var format=date.getFullYear()+"-"+(date.getMonth() + 1) +"-"+date.getDate();
Qualtrics.SurveyEngine.setEmbeddedData('m',format);
View originalLeave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.