0

Working on a PDF form in LiveCycle Designer, I have a field where employees will enter the proposed effective date for a schedule change. These can only be effective on the start of a pay period, which is every other Sunday. (For reference, Sunday, Jan 30, 2022 is a start of a pay period.)

I've tried very many ways of using if statements and the date.getDay() method, but I find that the getDay method sometimes returns the wrong number (the employee enters a date for a Sunday, but the code gets a getDay value of 1 instead of 0). I assume this has something to do with the local time, but not sure.

For reference, here is the code I currently have in my change event. I can't say for sure the rest of it works, as I can't get past the getDay problem. Anyway, appreciate either a fix to my getDay problem, or a more elegant solution to this whole problem that avoids it.

topmostSubform.Page1.Artifact[3].dateEffective::change - (JavaScript, client)

// Check if entered date is a Sunday at the start of a pay period.
// For reference, Sunday 1/30/2022 is the start of a pay period.

// perform this check only if something is entered in this field
xfa.host.messageBox('Value: ' + xfa.event.newText);
xfa.host.messageBox('Is an entry?: ' + !(!xfa.event.newText));
if (!(!xfa.event.newText)) {

    const myRefDate = new Date(2022, 1, 30);
    const myDate = new Date(xfa.event.newText);
    xfa.host.messageBox('Day: ' + myDate.getDay()); 
    
    // first check if entered date is a Sunday.  If so, then execute the other code.
    if (myDate.getDay() == 0) {

        const diffTime = Math.abs(myRefDate - myDate);
        xfa.host.messageBox('diffTime: ' + diffTime);
        const diffDays = diffTime / (1000 * 60 * 60 * 24); // includes fractional days
        xfa.host.messageBox('diffDays: ' + diffDays);
        const diffPP = diffDays / 14; // includes fractional parts of a 14-day pay period
        xfa.host.messageBox('diffPP: ' + diffPP);
        const remainderPP = diffPP % 1; // calculate remainder of diffPP
        xfa.host.messageBox('remainderPP: ' + remainderPP);
        
        // if the remainderPP value indicates that myDate is more than 1 day from a Sunday, then fail
        if (remainderPP * 14 > 1) {
            // messagebox with error
            xfa.host.messageBox('Error1: You must enter the date for a Sunday that is the start of a pay period.  Check your Pay/Holiday Schedule.');
            xfa.host.setFocus(this.name);
        }
    }else{
        xfa.host.messageBox('Error2: You must enter the date for a Sunday that is the start of a pay period.  Check your Pay/Holiday Schedule.');
        xfa.host.setFocus(this.name);
    }
    
};
Emily Beth
  • 709
  • 1
  • 7
  • 23
  • What is the value of 'xfa.event.newText' ? – Phalgun Jan 29 '22 at 23:10
  • @Phalgun, whatever they have entered into the field. For instance, if I (when entering data) select the date for two weeks from tomorrow, the messagebox that displays that shows "Value: 2/13/22" – Emily Beth Jan 29 '22 at 23:35
  • 1
    For which time zone is the above code is causing the issue? It seems similar to the problem described here -https://stackoverflow.com/a/54741311/3423750. The rule of thumb with dates is to process dates explicitly as UTC in code to avoid inconsistencies. Also, check if https://stackoverflow.com/a/5619588/3423750 helps. – Phalgun Jan 30 '22 at 00:12
  • @Phalgun Ugh, yes, you're right. I think the date picker for that date field is inserting UTC dates (even if they are displayed as local dates on my form), but the getDay() function is evaluating what the local day is assuming the UTC value. Who would have thought it would be so hard to just see if a date entered was a Sunday? Still lost, so if any other thoughts on how to check if a date entered in a field is a Sunday, please chime in! – Emily Beth Jan 30 '22 at 17:36
  • Since your dates are always constructed as UTC dates, can you use getUTCDay(). It will then be consistent across the date picker and your code. – Phalgun Jan 30 '22 at 19:00

0 Answers0