• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
18

Form field calculation - adding 2 weeks from today's date

Explorer ,
Feb 07, 2024 Feb 07, 2024

Copy link to clipboard

Copied

Hi there,
I'm looking to create a field that will autopopulate a date that is two weeks out from today's date. I've already created a field that autopopulates today's date.
In my form, the today's date field is named "Today", and the field that I require assistance with is named "Start Date". In other words, I would like the form's start date to always be 2 weeks from today's date.
Any support would be greatly appreciated!
Thanks 🙂

TOPICS
General troubleshooting , How to , JavaScript , PDF , PDF forms

Views

278

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 07, 2024 Feb 07, 2024

As 'Validate' script of "Today" field, use this: (set date format to whatever you need)

if (event.value == "")
 this.getField("Start Date").value = "";
else{
 var today = util.scand("mm/dd/yyyy", event.value);
 var startDate = new Date(today);
 startDate.setDate(today.getDate() + 14);
 this.getField("Start Date").value = util.printd("mm/dd/yyyy", startDate);}

Votes

Translate

Translate
Community Expert ,
Feb 07, 2024 Feb 07, 2024

Copy link to clipboard

Copied

As 'Validate' script of "Today" field, use this: (set date format to whatever you need)

if (event.value == "")
 this.getField("Start Date").value = "";
else{
 var today = util.scand("mm/dd/yyyy", event.value);
 var startDate = new Date(today);
 startDate.setDate(today.getDate() + 14);
 this.getField("Start Date").value = util.printd("mm/dd/yyyy", startDate);}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 07, 2024 Feb 07, 2024

Copy link to clipboard

Copied

Hi Nesa,

 

Thanks for the quick response! I added your suggested script to the 'properties' > 'validate' tab of the 'today' field, however the Start Date field is still not auto populating a date. Any ideas why this might be happening? Thanks!

 

Screenshot 2024-02-07 172407.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 08, 2024 Feb 08, 2024

Copy link to clipboard

Copied

It now seems to be working. Thanks again for your help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 29, 2024 Feb 29, 2024

Copy link to clipboard

Copied

Hi @Nesa Nurani - how would I code this if I wanted to insert it in a Document Level Script?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 29, 2024 Feb 29, 2024

Copy link to clipboard

Copied

Depends, are you trying to do the same thing just from document level, or use it in multiple fields?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 29, 2024 Feb 29, 2024

Copy link to clipboard

Copied

@Nesa Nurani Same thing just from document level. I want to ensure the script is run every time the document is opened.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 29, 2024 Feb 29, 2024

Copy link to clipboard

Copied

LATEST

Try this:

if (this.getField("Today").valueAsString == "")
 this.getField("Start Date").value = "";
else{
 var today = util.scand("mm/dd/yyyy", this.getField("Today").valueAsString);
 var startDate = new Date(today);
 startDate.setDate(today.getDate() + 14);
 this.getField("Start Date").value = util.printd("mm/dd/yyyy", startDate);}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines