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

Calculated Date Field (14+ days from date entered in another field)

Community Beginner ,
Mar 05, 2021 Mar 05, 2021

Fisrt off, I have spent several hours searching and trying different answers but nothing seems to work for this seemingly simple request.

 

I have two form fields, one is a date field (mm/dd/yyyy) the user will select the date for, and the other is a calculated date field that needs to add 14 days (same format) to the date the user selects. 

 

There have been some very complicated formulas I have tried and some very simple ones (Datefield1+14). I would think this could be done as a "Simplified Field Notation", however I cannot seem to find the formula to accomplish this...

TOPICS
JavaScript , PDF forms
14.0K
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Mar 05, 2021 Mar 05, 2021

As "Custom calculation script" of field where you select date use this.

var date = util.scand("mm/dd/yyyy", event.value);
if(event.value == "")
this.getField("Text1").value = "";
else {
date.setDate(date.getDate() +14);
this.getField("Text1").value = util.printd("mm/dd/yyyy", date);}

Change "Text1" to the name of the field where you want to add +14.

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 ,
Mar 05, 2021 Mar 05, 2021

Hi,

 

Just 2 cents on the above answer, I would use a "custom validation script", my reason for this is that a custom validation script is only called when the field changes, whereas a custom calculation script is called when any field changes and in this instance we only need the script to run when the actual value of field changes.

 

Malcolm

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 ,
Mar 14, 2024 Mar 14, 2024

Use script in the field where you enter event date, and replace "Event-Date" with field name in which you want to show date that is 90 days less.

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 Beginner ,
Mar 14, 2024 Mar 14, 2024

It is calculating, but what I need is for the calculations to begin after someone selects a date in "Text1"

It calculates from Today's date rather than a date value entered by the person filling the form - it actually needs to calculate five dates

 

Here is the complete script I used

 

var date = util.scand("mm/dd/yyyy", event.value);
if(event.value == "")
this.getField("Text2").value = "";
else {
date.setDate(date.getDate() -90);
this.getField("Text2").value = util.printd("mm/dd/yyyy", date);
}

var date = util.scand("mm/dd/yyyy", event.value);
if(event.value == "")
this.getField("Text3").value = "";
else {
date.setDate(date.getDate() -30);
this.getField("Text3").value = util.printd("mm/dd/yyyy", date);
}


var date = util.scand("mm/dd/yyyy", event.value);
if(event.value == "")
this.getField("Text4").value = "";
else {
date.setDate(date.getDate() -30);
this.getField("Text4").value = util.printd("mm/dd/yyyy", date);
}


var date = util.scand("mm/dd/yyyy", event.value);
if(event.value == "")
this.getField("Text5").value = "";
else {
date.setDate(date.getDate() -14);
this.getField("Text5").value = util.printd("mm/dd/yyyy", date);
}

var date = util.scand("mm/dd/yyyy", event.value);
if(event.value == "")
this.getField("Text6").value = "";
else {
date.setDate(date.getDate() -7);
this.getField("Text6").value = util.printd("mm/dd/yyyy", date);
}

 

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 ,
Mar 14, 2024 Mar 14, 2024

Use this script in field where you input date:

function setDateField(fieldName, daysOffset) {
 var date = util.scand("mm/dd/yyyy", event.value);
 if(event.value == "") {
  this.getField(fieldName).value = "";} 
 else {
  date.setDate(date.getDate() - daysOffset);
  this.getField(fieldName).value = util.printd("mm/dd/yyyy", date);}}

setDateField("Text2", 90);
setDateField("Text3", 30);
setDateField("Text4", 30);
setDateField("Text5", 14);
setDateField("Text6", 7);
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 Beginner ,
Mar 15, 2024 Mar 15, 2024

Thanks for the much simpler code, it still not triggering through the date input in the top text box. As soon as I paste in the code, it calculates all the dates based on today's date. How do I clear and reset the form, maybe that is the issue. I have attached a jpg to show what it is doing. 

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 ,
Mar 15, 2024 Mar 15, 2024

Do you have other scripts that you forgot to remove?

Use script as 'Validate'.

Share the file, photo doesn't help us.

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
New Here ,
Mar 28, 2024 Mar 28, 2024

Hi

how to add this formula in PDF 

=IF(D23="","",(EDATE(D23,1)-1))

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 Beginner ,
May 14, 2024 May 14, 2024
LATEST

Hello, So I use script: 
var date = util.scand("mm/dd/yyyy", event.value);
if(event.value == "")
this.getField("Text20").value = "";
else {
date.setDate(date.getDate() +7);
this.getField("Text20").value = util.printd("mm/dd/yyyy", date);}

to count out a week from a certain date. I have thirteen boxes with this code so that each box will consecutively display the next date that is 7 days out from the box that is in line before it. I am running into an issue after about 6 boxes. The date stops displaying after the sixth box. When I go to clear the starting week, the weeks in the 7th box finally shows up, but only displays the 7th, 8th, and 9th box. When I clear the data from these boxes, the remaining boxes display the correct dates. Im not sure what I can do to fix this. 

Also, would anyone know how to write a script that will display a specific day for a given week. For example: If the original starting week is week ending 5/11/24, could i get boxes to display the dates for monday, tuesday, wednesday, etc in designated boxes? 

Thank you

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