Advice on Moment.js and converting time to be Qualtrics readable | XM Community
Skip to main content
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
Hello @Meino ,



The date format which you want("YYYY-MM-DD") and which is written in code("MM-DD-YYYY") is different.
That is true - thanks; I'll change that around. I was playing with that to see if that made a difference. I just tested again to make sure that wasn't a reason for invalid date. Thanks so the proper full thing should read:



<code>Qualtrics.SurveyEngine.addOnReady(function()

<code>{

<code> var UNIX= Qualtrics.SurveyEngine.getEmbeddedData('UNIX');

<code> var dateString = moment('UNIX').format("YYYY-MM-DD");

<code> Qualtrics.SurveyEngine.setEmbeddedData('NEW_DATE', dateString);

<code>});
var initial = 'dd/mm/yyyy'.split(/\\//);

console.log( [ initial[1], initial[0], initial[2] ].join('/')); //=> 'mm/dd/yyyy'

// or in this case you could use

var initial = 'dd/mm/yyyy'.split(/\\//).reverse().join('/');
Hello @Meino

Try using this code:



var date = new Date(1516985915307);

var format=date.getFullYear()+"-"+(date.getMonth() + 1) +"-"+date.getDate();

Qualtrics.SurveyEngine.setEmbeddedData('NEW_DATE',format);



Also where are you using NEW_DATE

Note: Hope you have declared NEW_DATE in survey flow before setting it's value using js
@Shashi

Thanks for the help here. I took your code and it alone didn't work, but I edited it and got it to work, but it only worked once! After it worked, I changed the embedded data variable to a Date setting, and no further dates would be read. I then tried to create a new date embedded variable, but for some reason that didn't work. I even restored Qualtrics to the previous working version, but that didn't work either. So...I'm going to keep playing with it - I'm not exactly sure why it's inconsistent.



Here's what worked once!



<code>{

<code> var UNIX= Qualtrics.SurveyEngine.getEmbeddedData('UNIX');

<code>var dateString = moment('UNIX').date.getFullYear()+"-"+(date.getMonth() + 1) +"-<code>"+date.getDate();

<code>Qualtrics.SurveyEngine.setEmbeddedData('NEW_DATE', dateString);

<code>});
Yeah, I can't replicate this. If anyone has any further ideas, I'm all ears. I've tried moving the embedded variables above and below the block. It first worked when they were both above the block.
> @Meino said:

> Yeah, I can't replicate this. If anyone has any further ideas, I'm all ears. I've tried moving the embedded variables above and below the block. It first worked when they were both above the block.

(keep above the block)

Please make sure your flow is as follows:



In Survey flow create embedded data before the question on which you are applying js. Then use that embedded data where required.

Also where are you using your embedded data?
Sure I'll keep them both above.



I'm not actually using the embedded data in the survey to take any action, I'm just trying recording the date properly for Qualtrics, so that I I can properly filter within Reports or on a Vocalize Dashboard by date - e..g. filter by a month ago, filter by a year ago, etc etc.
For vocalize there is an option in vocalize to change the date format in recode symbol. I suggest you dont need to reorder date here.
> @Priyanka_jain said:

> For vocalize there is an option in vocalize to change the date format in recode symbol. I suggest you dont need to reorder date here.



@Meino Please see this
Hi @Shashi and @Priyanka_jain thanks for the advice. The dates I'm receiving in the survey will be in UNIX format like: 1516985915307. Even with custom date formatting, I don't believe there's a way to recode properly.



I also am using a webservice to convert the dates: 1516985915307 becomes:

Saturday 27th January 2018 12:04:44 AM, so I'm trying to convert that using custom formatting in Qualtrics Vocalize, but I think I'm having issues with the "th" in 27th. This is what I was trying:



E e M YYYY HH🇲🇲ss a
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);
Thank you @Shashi - much appreciated. Just to wrap it up for any readers, that code will output the proper yyyy-mm-dd format to then recode in Vocalize. Make sure you put it under: Qualtrics.SurveyEngine.addOnload - not OnReady for consistent firing (in my experience).

Leave a Reply