© 2024 IQVIA - All Rights Reserved

clinsparkDatesToMilliseconds

Summary

ClinSpark Items supports the following DataTypes which contain date/time information:

  • Date

  • Time

  • DateTime

  • Incomplete Date

  • Incomplete Time

  • Incomplete DateTime

A frequent task in edit checks is to compare these values, including scenarios where a type contains incomplete values.

This utility applies normalizing logic across each of these datatypes, including setting missing fields from incomplete datatypes to the appropriate “zero-like” equivalent (this differs by field since first month is 0, but first day i 1).

It returns a ms value which is useful for comparing relative times, such as which value is first.

Formal Expression

var first = findFirstItemByName(formJson, 'incomplete DateTime 1'); var second = findFirstItemByName(formJson, 'incomplete DateTime 2'); return clinsparkDatesToMilliseconds(first) >= clinsparkDatesToMilliseconds(second); /* Extracts the time in milliseconds for ClinSpark item DataTypes including DateTime, Date, Time and the incomplete versions of each of these. Expects that the dataType has a date component. May not be meaningful if the DateTypes do not match. */ function clinsparkDatesToMilliseconds(itemData) { var dateFields = toDateFields(itemData); var timeFields = toTimeFields(itemData); // logger('input value '+itemData.value); // logger('Date fields '+dateFields); // logger('Time fields '+timeFields); var date = new Date(dateFields[0],dateFields[1],dateFields[2],timeFields[0],timeFields[1],timeFields[2]); // logger('ms: '+date.getTime()+' '+date); return date.getTime(); } /* Extracts the date fields from an itemData which contains them. For incomplete type blank fields are replaced with 1 */ function toDateFields(item) { var value = item.value; var d = [0,1,1]; if (item.dataType.toLowerCase().indexOf('datetime') != -1) { d = value.split('T')[0].split('-'); } else if (item.dataType.toLowerCase().indexOf('date') != -1) { d = value.split('-'); } d = defautBlankArrayElements(d, 1); d[1] = d[1] -1; // // date field is 0 based return d; } /* Extracts the time fields from an itemData which contains them. For incomplete type blank fields are replaced with 0 */ function toTimeFields(item) { var value = item.value; var d = [0,0,0]; if (item.dataType.toLowerCase().indexOf('datetime') != -1) { d = value.split('T')[1].split(':'); } else if (item.dataType.toLowerCase().indexOf('time') != -1) { d = value.split(':'); } d = defautBlankArrayElements(d, 0); return d; } function defautBlankArrayElements(array, defaultInt, isDate) { for (var i=0; i < array.length; i++) { if (typeof array[i] === 'undefined' || (typeof array[i] == 'string' && array[i].trim().length == 0)) { array[i] = defaultInt; } } return array; }

Exported and Printed Copies Are Uncontrolled