Embedded Data - How to format date of survey response as "week of week year" in survey platform | XM Community
Question

Embedded Data - How to format date of survey response as "week of week year" in survey platform

  • 7 November 2019
  • 1 reply
  • 191 views

Badge +2
Hi all. I want to take the date the respondent completed a survey and reformat that date as "week year" for a calculation I need to do. I want to do this within embedded data if at all possible

Basically, if a respondent completes the survey on Jan. 8, I want the embedded data field to read "2" and if they complete the survey on Dec. 28, I want the embedded data field to read "52".

I see in CX platform this is possible with a list of custom formats, and I want to use the custom format that is called "week of week year" (w), but I cannot figure out how to do that same thing in embedded data on the survey side (which would allow me to do more calculations right within the embedded data fields).

Here is what I presently tried -- it is not working.
!

Obviously I'm new to Qualtrics with this kind of question. If anybody can help with this it would be so appreciated.

1 reply

Userlevel 4
Badge +7
Hi @brianyearling!

One approach would be to use JavaScript to determine the week number.

First, add an embedded data field (e.g., 'weekNumber') to the Survey Flow. It is recommended to put this at the very top of the survey flow so that the field is available to be set by the Javascript.

Next, add the following JavaScript to a question in the survey where you want the week number to be captured. I've used the embedded data field called 'weekNumber' here but you can update the JavaScript to refer to whatever you decide to call your embedded data field.

```javascript
Qualtrics.SurveyEngine.addOnload(function()
{

Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(), 0, 1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay() + 1) / 7);
}

var weekNumber = (new Date()).getWeek();

Qualtrics.SurveyEngine.setEmbeddedData('weekNumber', weekNumber);

});
```

Leave a Reply