Need help calculating the difference between current date and screening date format(mm/yyyy) | XM Community
Skip to main content
I'm working on setting up a screening form for a breast cancer study. One of the questions for eligibility a recent mammogram.



1. When was your most recent mammogram? [Month/Year]



If the mammogram was more than two years ago the patient is not eligible. Is there a way to calculate the difference from today's date and the mammogram date in years when the data entry text box is only capturing the month and year? Any assistance would be much appreciated.



Thanks!
Assume the mammogram date was the first of the month. I recommend using moment.js for date calculations.
@TomG Thanks for responding. I saw your previous posts related to date calcs.



I'm stuck on how to proceed.



So far I've included this script into the html: <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>.



I then created an ED field to pipe the month and year variables; assuming the day is 01: mammodate=${q://QID43/ChoiceTextEntryValue/1/1}/01/${q://QID43/ChoiceTextEntryValue/1/2}



I also created an ED field for the number of years (mammoyrs) and designating eligibility (mammoelig) for anyone with dates less than 2 years.



Using your answer to a previous question, I edited the script to this.



Qualtrics.SurveyEngine.addOnPageSubmit(function() {

var mammoyrs = moment().diff(moment(jQuery("#"+this.questionId+" .InputText").val()), 'years');

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

if(mammoyrs > 2) Qualtrics.SurveyEngine.setEmbeddedData('mammoelig', '1');

});





Any insight as to what I'm doing wrong??



Thanks!!
It isn't clear to me what question your script is attached to, but if you already have the mammography date in the embedded variable mammodate, then the first line of the script would be:

```

var mammoyrs = moment().diff(moment("${e://Field/mammodate}","MM-DD-YYYY"), 'years');

```

Leave a Reply