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

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

Community Beginner ,
Mar 05, 2021 Mar 05, 2021

Copy link to clipboard

Copied

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

Views

8.8K

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 2 Correct answers

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.

Votes

Translate

Translate
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

Votes

Translate

Translate
Community Expert ,
Mar 05, 2021 Mar 05, 2021

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

I tried it as both custom validation and custom calculation and it does nothing... 

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

Copy link to clipboard

Copied

Annnnd I know why... I was putting it in the field that I want to be automatically updated and pointing it to the field the user enters, not the other way around. This works! Thank you!

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
New Here ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

I used this code and it works perfectly. How can I use this same script, but exclude Sundays?

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 ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

Hi,

 

What do you mean "exclude Sundays"?

 

Do you mean if the calculated date is a Sunday, they make it the Monday?

OR

If there is a Sunday passed during the calculcation add another day?

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 ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

Just add two extra days... If you add 14 days, then there must be 2 Sundays within that period, no matter what.

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
New Here ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

If I need two text fields to display, for example, "Text1 for 14 days and Text2 for 30 days, is it possible??

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 ,
Jun 20, 2022 Jun 20, 2022

Copy link to clipboard

Copied

Hi,

 

Just duplicate the dates lines and change the 14 to 30 and change the name of date to secondDate or date2 or some other name.

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
New Here ,
Oct 20, 2022 Oct 20, 2022

Copy link to clipboard

Copied

Would it be possible to use this same formula, but instead of having a set amount of days (such as 14) using another field on the form that would be populated with any number?  

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 ,
Oct 20, 2022 Oct 20, 2022

Copy link to clipboard

Copied

Yes, put field value in variable and replace 14 with variable name, something like this.

var nDays = Number(this.getField("Field name").valueAsString);

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
New Here ,
Oct 20, 2022 Oct 20, 2022

Copy link to clipboard

Copied

Forgive me, but I'm not entirely clear where that addition would go 🙂

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 ,
Oct 20, 2022 Oct 20, 2022

Copy link to clipboard

Copied

You can put it on top of the script, change "Field name" with your actual name of the field from which you wish to acquire number and replace 14 with nDays.

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
New Here ,
Oct 20, 2022 Oct 20, 2022

Copy link to clipboard

Copied

I updated the script based on the field names.  Days = the variable field that will have a number entered and Date3 is the the field that has the date that the number of days should be added to.  I am entering the script under custom calculation script and it isn't working.  

 

var nDays = Number(this.getField("Days").valueAsString);
var date = util.scand("mm/dd/yyyy", event.value);
if(event.value == "")
this.getField("Date3").value = "";
else {
date.setDate(date.getDate() +nDays);
this.getField("Date3").value = util.printd("mm/dd/yyyy", date);}

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 ,
Oct 20, 2022 Oct 20, 2022

Copy link to clipboard

Copied

It should work fine, check console for errors.

In what field did you add script?

If you can't figure it out, please share your file with us.

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
New Here ,
Aug 14, 2023 Aug 14, 2023

Copy link to clipboard

Copied

Hello
This is great - so helpful.
I can use the above code to calculate a date for one field; I have a list that I need to calculate dates for though - all using the same start date field.
I tried to just copy and paste the first code below (with the amended field name) but received an error.  
Is this possible please? 

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 ,
Aug 15, 2023 Aug 15, 2023

Copy link to clipboard

Copied

Yes, it's possible. What did the error message you got said?

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
New Here ,
Aug 15, 2023 Aug 15, 2023

Copy link to clipboard

Copied

Apologies - just realised it's because I used +0, when I used +1 it worked. Are you able to assist with what to use if I want it to reflect the same date please? 
Many thanks, 

Carly 

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

Copy link to clipboard

Copied

This is very helpful, but I need more help. I am setting up a form that calculates event timelines for promotion benchmarks. A person will type in their event date (i.e. 12/25/2024) and the next field will subtract 90 days to let the person know when their Design Brief is due (i.e. 09/26/2024). The code worked but it is figuring from "Today's Date" rather than the date someone inputs into the form. 

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

Copy link to clipboard

Copied

Post the script you tried.

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

Copy link to clipboard

Copied

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

 

I am assuming "Event-Date" means today's date. I need it to calculate from the date selected by the person filling the form.

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

Copy link to clipboard

Copied

No, it's a field name. What is the name of the field that holds the date you want to calculate from?

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

Copy link to clipboard

Copied

I grabbed the wrong script. My event date will be "Text1" (a date the user inputs) that subtracts 90 days for "Text2"

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

Copy link to clipboard

Copied

As the custom calculation script of "Text2" use the following code, then:

 

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

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