Hi All, (sorry title should have read ‘JS code to calc number of days between two dates but sometimes blank’)
I am seeing some intermittent issues in calculating the difference between two dates using some JS code in a question that is shown at the beginning of my survey.
Below is a pic of some example data showing the issue:

Both the Contact_DIR_Default_Prophet_Segment_Lastmodified and TodayDate embedded fields are being populated ok but sometimes the Days_Since_Prophet_Updated field is not being calculated.
I’m aware that often if JS code fails for one reason or another it can return just a blank / null value and so I’m wondering if there is a way I can capture an error code if it fails to understand why. Is that possible?
I’ve manually tested the dates that didn’t work above (console window of my chrome browser) that didn’t show a figure and they work fine.

Below is my setup in order of sequence:
- Survey flow: TodayDate is populated and Days_Since_Prophet_Update field created
- Survey Flow: Webservice API call looks up contact from a directory and returns the field: Contact_DIR_Default_Prophet_Segment_Lastmodified. (FYI If the call fails it repeats 3 times through a simple branch logic).
- Display block 0c with (intro / welcome letter) with a Text/Graphic question to welcome the respondent and execute the following JS code:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
//Calulate time since Prophet Segmment was last updated
var date1 = new Date("${e://Field/Contact_DIR_Default_Prophet_Segment_Lastmodified}").getTime();
var date2 = new Date("${e://Field/TodayDate}").getTime();
var diff = parseInt(date2 - date1);
var days = parseInt(diff/1000/60/60/24);
//var days = diff;
Qualtrics.SurveyEngine.setEmbeddedData("Days_Since_Prophet_Updated", days);
}); -
next block (block 0d) but this only displays when it’s a preview but if you review the response (of any type) in the D&A section you can review the embedded data fields etc. but it’s just greyed out. I don’t believe this has any impact in the issues experienced but I’m just mentioning this for completeness
the below pic is from one which didn’t populate
Recorded: September 10, 2025 9:13 AM
Duration: 00:24:09
the below pic is from one which did populate
Recorded: September 10, 2025 8:27 AM
Duration: 00:04:25 -
next block (block 1) NPS and other questions…….
So that is the process. I went to check to see if there was any pattern regarding the browser metadata (browser version devices etc) but unfortunately I’d not taken those details. I’ve now implemented that question and will come back with some more info if I see a pattern.
But in the meantime, hope someone can advise or can confirm they’ve experienced this sporadic problem too.
Thanks
Rod Pestell