Search code examples
javascriptadobe

Adobe JavaScript- Check a box if the date entered is older than 365 days


I am trying to create either a Validation Script or a Javascript Action in Adobe.

I need to check a box called CheckBox2 is the Date entered in DORMANT is greater than 1 year old from Today's date.

So if Date entered is 06/02/2021 then the box would be checked.

I have googled so many things and not found an answer that works.
Any help would be great.

Thank you


Solution

  • // Get the value of the DORMANT field
    var dormantDateStr = this.getField("DORMANT").value;
    
    // Parse the date entered in the DORMANT field
    var dormantDate = util.scand("mm/dd/yyyy", dormantDateStr); 
    //ensure the date format here matches the date in your field DORMANT
    
    // Get today's date
    var today = new Date();
    
    // Calculate the date one year ago from today
    var oneYearAgo = new Date();
    oneYearAgo.setFullYear(today.getFullYear() - 1); 
    
    
    // Check if the dormant date is more than one year ago
    if (dormantDate < oneYearAgo) {
        // Check the CheckBox2
        this.getField("CheckBox2").checkThisBox(0, true);
    } else {
        // Uncheck the CheckBox2 if the condition is not met
        this.getField("CheckBox2").checkThisBox(0, false);
    }
    

    getFullYear

    Also, you could add this as a JS script in Adobe instead of a validation script, can be triggered when DORMANT is filled and loses focus etc. Adobe Acrobat > Tools > JavaScript > Document JavaScripts > Add