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

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

Explorer ,
Feb 07, 2024 Feb 07, 2024

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
1.6K
Translate
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
1 ACCEPTED SOLUTION
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);}

View solution in original post

Translate
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 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);}
Translate
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

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

Translate
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

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

Translate
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

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

Translate
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

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

Translate
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

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

Translate
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
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);}
Translate
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