Reformatting a Date | XM Community
Skip to main content

I have a question that asks for a date in the standard U.S. format: MM/DD/YYYY. Using JS, I got the text entry exactly how I wanted (see screenshot below).

Question’s JavaScript

 

What was also awesome was that it even though it presented the date in the standard U.S. format, it saved the date in the international format: YYYY-MM-DD. This allowed me to perform date comparisons in display logic further along in the survey, using embedded data (see screenshot below).

Comparison Display Logic

 

One thing that I have been asked to do from here, however, is to have another text/graphic “question” at the end of the survey that shows the saved date in the standard U.S. format--essentially converting the date back into the form it was initially selected in. I imagine that piped text and embedded data would allow me to easily convert the YYYY-MM-DD date back to MM/DD/YYYY, but I am struggling to figure out the code that achieves this reformatting. Anyone know a quick one- or two-liner that can switch the display around?

@JasonMHuynh Add this Java Script to your text/graphic, change the pipe-text according to your question

Qualtrics.SurveyEngine.addOnload(function() {

var dateStr = "${q://QID1/ChoiceTextEntryValue}"; //!!!!!Change this field to yours!!!!


if (dateStr) {
var dateParts = dateStr.split("-");
var formattedDate = datePartss1] + "/" + datePartss2] + "/" + datePartss0];
document.getElementById("formattedDate").innerText = formattedDate;
}
});

In your text, go Rick Content Editor → Source mode and insert this code where you want your date to appear

<p id="formattedDate"></p>

Let me know if it work


Thank you, Nam! I forgot about the split function. This solution works perfectly.


Leave a Reply