Skip to main content
Participating Frequently
July 3, 2023
Answered

PDF Forms - Auto calculate the week ending date of Sunday based of the weeks work date

  • July 3, 2023
  • 2 replies
  • 1689 views

I am trying to auto populate the week ending date, which is Sunday, based on the weeks work date that my tech did the work. I am new to forms and not a script wizard. Any help would be greatly appreciated. 

 

 

This topic has been closed for replies.
Correct answer Thom Parker

The full script will need to be a calculation script in the "Week End Date" field.

Remove the date formatting from this field, and make it ReadOnly. Formatting is only needed when dates are entered manually.

The input date will need to be created using the "util.scand" function. And the Output will need to be formatted using util.printd.  Like this (same script just using scand and printd to handle the date formatting)

 

 


var strWorkDate = this.getField("Work Date").value;
if(strWorkDate != "")
{
    var dtStart = util.scand("m/d/yyyy",strWorkDate);
    // Sunday is 0, Monday is 1, etc. 
    var nDayOfWeek = dtStart .getDay(); 

    var dtEndOfWeek = null;
    if(nDayOfWeek == 0)
    {// Today is Sunday, so today is the end of the week
        dtEndOfWeek = dtStart ;
    }
    else
    {// Find number of days until next sunday, then add to current date
        nOffsetToSunday = 7-nDayOfWeek;
        nSundayDateTime = dtStart.setDate(dtStart.getDate() + nOffsetToSunday);
        dtEndOfWeek = new Date(nSundayDateTime);
    }
    event.value = util.printd("m/d/yyyy",dtEndOfWeek);
}
else
    event.value = "";

 

 

2 replies

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
July 3, 2023

The full script will need to be a calculation script in the "Week End Date" field.

Remove the date formatting from this field, and make it ReadOnly. Formatting is only needed when dates are entered manually.

The input date will need to be created using the "util.scand" function. And the Output will need to be formatted using util.printd.  Like this (same script just using scand and printd to handle the date formatting)

 

 


var strWorkDate = this.getField("Work Date").value;
if(strWorkDate != "")
{
    var dtStart = util.scand("m/d/yyyy",strWorkDate);
    // Sunday is 0, Monday is 1, etc. 
    var nDayOfWeek = dtStart .getDay(); 

    var dtEndOfWeek = null;
    if(nDayOfWeek == 0)
    {// Today is Sunday, so today is the end of the week
        dtEndOfWeek = dtStart ;
    }
    else
    {// Find number of days until next sunday, then add to current date
        nOffsetToSunday = 7-nDayOfWeek;
        nSundayDateTime = dtStart.setDate(dtStart.getDate() + nOffsetToSunday);
        dtEndOfWeek = new Date(nSundayDateTime);
    }
    event.value = util.printd("m/d/yyyy",dtEndOfWeek);
}
else
    event.value = "";

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
July 3, 2023

You ROCK, thank you so much. I will let you know how it turns out. 🙂

Thom Parker
Community Expert
Community Expert
July 3, 2023

I just did an update to the script to fix a syntax error, make sure you get the correction.

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Community Expert
Community Expert
July 3, 2023

 

To find the date for the next Sunday you need to first figure out the number of days from today until the next sunday.

 

This code does just than based on the current date/time.

 

var dtNow = new Date();
// Sunday is 0, Monday is 1, etc. 
var nDayOfWeek = dtNow.getDay(); 

var dtEndOfWeek = null;
if(nDayOfWeek == 0)
{// Today is Sunday, so today is the end of the week
    dtEndOfWeek = dtNow;
}
else
{// Find number of days until next sunday, then add to current date
    nOffsetToSunday = 7-nDayOfWeek;
    nSundayDateTime = dtNow.setDate(dtNow.getDate() + nOffsetToSunday);
    dtEndOfWeek = new Date(nSundayDateTime);
}

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
July 3, 2023

Thank you, I will try this.  I did see another post that added script to leave week end date blank until the referenced date was populated.  Will what you provided work if I figure out how to add that script with it?