Number of days between selected date and fixed date

Community Beginner ,
Jun 20, 2022 Jun 20, 2022

Copy link to clipboard

Copied

I'm part of a private school with primarily minority students where the annual tuition of $9,500 varies by the student's start date. 

 

There are two form fields with which I am needing help. One is a date field ("StartDate") for which the user selects the date on the date picker dropdown. The other field ("Tuition"), I need to equal the product of the number of days between the date the user selects until June 2, 2023 and then multiplied by 32.647. Can you please help me with this?

TOPICS
JavaScript , PDF forms

Views

119

Likes

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

Adobe Community Professional , Jun 20, 2022 Jun 20, 2022

Add code to test the start date field. If it is empty, then set the ouput to 0 or a blank.

 

var strStartDate = this.getField("StartDate").valueAsString;
if(strStartDate != "")
{
    var nDays = TimeDateDiff(strStartDate, "June 2, 2023", "mmm dd, yyyy", 4);
    event.value = nDays * 32.647;
}
else
    event.value = 0;


 

 

Likes

Translate

Translate
Adobe Community Professional , Jun 21, 2022 Jun 21, 2022

My mistake, typing too fast. 

The input to the "new Date" constructor needs to be a string. Debug skills are important. If you are going to copy someone elses code you should have some idea of how it works and how to fix it. 

 

 

var strStart = this.getField("StartDate").valueAsString;
if(strStart != "")
{
    var dtStart = util.scand("mmm dd, yyyy", strStart);
    if(dtStart > (new Date("6/2/2023")))
       event.value = 0;
    else if(dtStart > (new Date("3/9/2023")))
       event.value = 0.25
...

Likes

Translate

Translate
Adobe Community Professional , Jun 21, 2022 Jun 21, 2022

Just to add to Thom Parker solution, add variable to the top of the script for scholarship field and then multiply percentage by that variable, something like this:

var strStart = this.getField("StartDate").valueAsString;
var sch = Number(this.getField("FullScholarship").value);
if(strStart != "")
{
var dtStart = util.scand("mmm dd, yyyy", strStart);
if(dtStart > (new Date("6/2/2023")))
event.value = 0;
else if(dtStart > (new Date("3/9/2023")))
event.value = 0.25*sch; //...etc

 

Likes

Translate

Translate
Adobe Community Professional ,
Jun 20, 2022 Jun 20, 2022

Copy link to clipboard

Copied

So you need to write a custom calculation script. This article will help you get started:

https://www.pdfscripting.com/public/How-to-Write-a-Basic-PDF-Calculation-Script.cfm

 

However, you have an additional difficulty because of the dates. Dates are not easy to deal with To help with this, here is a function that calculates the difference between two date/times and returns the result as the number of selected time units (seconds, minutes, etc).

 

 

 

function TimeDateDiff(strDate1, strDate2, strInFormat, nOutput)
{
	if(!nOutput)
		nOutput = 0;
	
	if(!strInFormat || (strInFormat == ""))
		strInFormat = "mm/dd/yyyy HH:MM:ss";
	
	var nRtn = null;
	var bNeg = false;
	
	var dt1 = util.scand(strInFormat, strDate1);
	var dt2 = util.scand(strInFormat, strDate2);
	if(dt1 > dt2){
		// Swap for negative difference
		var dtTmp = dt2;
		dt2 = dt1;
		dt1 = dtTmp;
		bNeg = true;
	}
	if(dt1 && dt2){
		var nDiff = (dt2-dt1);
		switch(nOutput)
		{
			case 0:// Return miliseconds
				nRtn = nDiff;
				break;
			case 1:// Return seconds
				nRtn = nDiff/60000;
				break;
			case 2:// Return minutes
				nRtn = nDiff/60000;
				break;
			case 3:// Return hours
				nRtn = nDiff/3600000;
				break;
			case 4:// Return days
				nRtn = nDiff/86400000;
				break;
			case 5:// Return weeks
				nRtn = nDiff/604800000;
				break;
		}
		// round to the nearest 100th
		nRtn = Math.round(nRtn*100)/100;
	}
	return nRtn;
}

	

 

 

 

Notice that the input to this function is two date/times represented at text strings. These text strings need to be converted into date objects, so it is important to specify the date/time format of the text.  You can find info on date formatting here:

https://www.pdfscripting.com/public/Date-and-Time-Handling.cfm

 

for example, for the date you specified you can use this code in a calculation script.  The start date has to be in the same format as the end date you've specified.

 

 

var strStartDate = this.getField("StartDate").value;
var nDays = TimeDateDiff(strStartDate, "June 2, 2023", "mmm dd, yyyy", 4);

event.value = nDays * 32.647;

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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

Copy link to clipboard

Copied

This works perfectly except when I clear the form, the "Tuition" field populates with 11,312. How can I make sure it's remains blank when no date has been selected?

Likes

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

Copy link to clipboard

Copied

Add code to test the start date field. If it is empty, then set the ouput to 0 or a blank.

 

var strStartDate = this.getField("StartDate").valueAsString;
if(strStartDate != "")
{
    var nDays = TimeDateDiff(strStartDate, "June 2, 2023", "mmm dd, yyyy", 4);
    event.value = nDays * 32.647;
}
else
    event.value = 0;


 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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

Copy link to clipboard

Copied

Thank you so much! What a blessing!

Likes

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

Copy link to clipboard

Copied

Hello, Mr. Parker. Thank you again for your great help yesterday! That's huge and will be a big blessing to our Campus Directors, Parents and Students. One last request (if you'll indulge me):

Another field, "StateScholarship", populates with the scholarship amount a student is entitled to that should calculate, predicated on the "StartDate" field. The scholarship amount a student receives varies by quarters.

A start date from:

  • Aug 15, 2022 - Oct 7, 2022, they receive 100% of scholarship
  • Oct 7, 2022 - Dec 9, 2022, they receive 75% of scholarship
  • Dec 10, 2022 - Mar 9, 2023, they receive 50% of scholarship
  • Mar 10, 2023 - Jun 2, 2023, they receive 25% of scholarship

How would this be written?

Likes

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
Adobe Community Professional ,
Jun 21, 2022 Jun 21, 2022

Copy link to clipboard

Copied

This one is a simple comparison. Date objects can be compared directly, just line regular numbers. 

Here's a calculation script for the percentage.  Notice how the comparions are laddered. 


var strStart = this.getField("StartDate").valueAsString;
if(strStart != "")
{
    var dtStart = util.scand("mmm dd, yyyy", strStart);
    if(dtStart > (new Date(6/2/2023)))
       event.value = 0;
    else if(dtStart > (new Date(3/9/2023)))
       event.value = 0.25;
    else if(dtStart > (new Date(12/9/2022)))
       event.value = 0.5;
    else if(dtStart > (new Date(10/7/2022)))
       event.value = 0.75;
    else if(dtStart > (new Date(8/14/2022)))
       event.value = 1;
    else // ? What if it's earlier?//
       event.value = 1;
}
else
   event.value = "";

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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

Copy link to clipboard

Copied

Thanks, Mr. Parker.

I entered this script and get nothing in the field. I'm pretty sure there should be code that multiplies these .25, .5, .75, etc. by the amount in the field "FullScholarship" (it populates the scholarship dollar amount when someone selects which scholarship on the dropdown).

Likes

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
Adobe Community Professional ,
Jun 21, 2022 Jun 21, 2022

Copy link to clipboard

Copied

Just to add to Thom Parker solution, add variable to the top of the script for scholarship field and then multiply percentage by that variable, something like this:

var strStart = this.getField("StartDate").valueAsString;
var sch = Number(this.getField("FullScholarship").value);
if(strStart != "")
{
var dtStart = util.scand("mmm dd, yyyy", strStart);
if(dtStart > (new Date("6/2/2023")))
event.value = 0;
else if(dtStart > (new Date("3/9/2023")))
event.value = 0.25*sch; //...etc

 

Likes

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

Copy link to clipboard

Copied

LATEST

Genius! Thank you!!

Likes

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
Adobe Community Professional ,
Jun 21, 2022 Jun 21, 2022

Copy link to clipboard

Copied

My mistake, typing too fast. 

The input to the "new Date" constructor needs to be a string. Debug skills are important. If you are going to copy someone elses code you should have some idea of how it works and how to fix it. 

 

 

var strStart = this.getField("StartDate").valueAsString;
if(strStart != "")
{
    var dtStart = util.scand("mmm dd, yyyy", strStart);
    if(dtStart > (new Date("6/2/2023")))
       event.value = 0;
    else if(dtStart > (new Date("3/9/2023")))
       event.value = 0.25;
    else if(dtStart > (new Date("12/9/2022")))
       event.value = 0.5;
    else if(dtStart > (new Date("10/7/2022")))
       event.value = 0.75;
    else if(dtStart > (new Date("8/14/2022")))
       event.value = 1;
    else // ? What if it's earlier?//
       event.value = 1;
}
else
   event.value = "";

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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

Copy link to clipboard

Copied

Brilliant! Thank you!!

Likes

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
Enthusiast ,
Jun 21, 2022 Jun 21, 2022

Copy link to clipboard

Copied

Not used for this answer, but for the "case 1" of the function script (Returns seconds) nDiff must be divided by 1000 instead of 60000 (from milliseconds to seconds).

				...
				break;
			case 1:// Return seconds
				nRtn = nDiff/1000;
				break;
				...

 @+

Likes

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
Adobe Community Professional ,
Jun 21, 2022 Jun 21, 2022

Copy link to clipboard

Copied

quote

Not used for this answer, but for the "case 1" of the function script (Returns seconds) nDiff must be divided by 1000 instead of 60000 (from milliseconds to seconds).

 @+


By @bebarth


Good Catch, Thanks!!

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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