Versions Compared

Key

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

...

Code Block
languagejs
function isLogicalIncompleteDatetime(value) {
    // Split the datetime into date and time parts
    var parts = value.split('T');
    var datePart = parts[0].split('-');
    var timePart = parts.length > 1 ? parts[1].split(':') : [];

    // Define helper function to check if an array has any missing parts
    function hasMissingParts(arr) {
        return arr.some(function (part) {
            return part === null || part.trim() === '';
        });
    }

    // Check for logical date parts
    if (datePart.length === 3) {
        // Year should not be missing
        if (datePart[0].trim() === '') {
            customErrorMessage('Year is missing, but month or day is present.');
            return false;
        }

        // If month is present, year should be present
        if (datePart[1].trim() !== '' && datePart[0].trim() === '') {
            customErrorMessage('Month is present, but year is missing.');
            return false;
        }

        // If day is present, both year and month should be present
        if (datePart[2].trim() !== '' && (datePart[0].trim() === '' || datePart[1].trim() === '')) {
            customErrorMessage('Day is present, but year or month is missing.');
            return false;
        }
    }

    // Check for logical time parts
    if (timePart.length > 0) {
        // Hour should not be missing if minutes or seconds are present
        if (timePart[0].trim() === '' && (timePart[1].trim() !== '' || timePart.length > 2 && timePart[2].trim() !== '')) {
            customErrorMessage('Hour is missing, but minute or second is present.');
            return false;
        }

        // If minutes are present, hour should be present
        if (timePart[1].trim() !== '' && timePart[0].trim() === '') {
            customErrorMessage('Minute is present, but hour is missing.');
            return false;
        }

        // If seconds are present, both hour and minute should be present
        if (timePart.length > 2 && timePart[2].trim() !== '' && (timePart[0].trim() === '' || timePart[1].trim() === '')) {
            customErrorMessage('Second is present, but hour or minute is missing.');
            return false;
        }
    }

    // If all checks pass, the datetime is logically consistent
    return true;
}

// Execute the check on the item's value
var isValid = isLogicalIncompleteDatetime(itemJson.item.value);
return isValid;

Edit Check Walkthrough

...