Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
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;
}  

...