Time Caculation - Calculated field value still remains after resetting all the fields.

Contributor ,
May 19, 2020

Copy link to clipboard

Copied

Hi,

I got this script of calculation of time 

 

on this community and i modified it to suit my form everything is working fine.. but when i reset the form .. everything should be blank or 0 but  the calculated field still keeps the last calculated value;

 

Here is the script (run through calculate tab): 

 

var cStartDate = this.getField("Date").valueAsString;
var cStartTime1 = this.getField("IN_TIME1").valueAsString;
var cStartDateFormat = "dd-mmm-yyyy";
var cStartTimeFormat = "h:MM tt";
cStartTime1 = AdjustMidnight(cStartTime1);

var cEndDate = this.getField("Date").valueAsString;
var cEndTime1 = this.getField("OUT_TIME1").valueAsString;
var cEndDateFormat = "dd-mmm-yyyy";
var cEndTimeFormat = "h:MM tt";
cEndTime1 = AdjustMidnight(cEndTime1);

event.value = 0;
if(cStartDate != "" && cStartTime1 != "" && cEndDate != "" && cEndTime1 != "")
{
var t1 = DateTime2Min(cEndDateFormat + " " + cEndTimeFormat, cEndDate + " " + cEndTime1) - DateTime2Min(cStartDateFormat + " " + cStartTimeFormat, cStartDate + " " + cStartTime1);
}


var cStartDate = this.getField("Date").valueAsString;
var cStartTime2 = this.getField("IN_TIME2").valueAsString;
var cStartDateFormat = "dd-mmm-yyyy";
var cStartTimeFormat = "h:MM tt";
cStartTime2 = AdjustMidnight(cStartTime2);

var cEndDate = this.getField("Date").valueAsString;
var cEndTime2 = this.getField("OUT_TIME2").valueAsString;
var cEndDateFormat = "dd-mmm-yyyy";
var cEndTimeFormat = "h:MM tt";
cEndTime2 = AdjustMidnight(cEndTime2);

event.value = 0;

if(cStartDate != "" && cStartTime2 != "" && cEndDate != "" && cEndTime2 != "")
{
var t2 = DateTime2Min(cEndDateFormat + " " + cEndTimeFormat, cEndDate + " " + cEndTime2) - DateTime2Min(cStartDateFormat + " " + cStartTimeFormat, cStartDate + " " + cStartTime2);
}

event.value = t1 + t2;

 

 

 

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

Try this:

var t1 = "";
var t2 = "";
var cStartDate = this.getField("Date").valueAsString;
var cStartTime1 = this.getField("IN_TIME1").valueAsString;
var cStartDateFormat = "dd-mmm-yyyy";
var cStartTimeFormat = "h:MM tt";
cStartTime1 = AdjustMidnight(cStartTime1);

var cEndDate = this.getField("Date").valueAsString;
var cEndTime1 = this.getField("OUT_TIME1").valueAsString;
var cEndDateFormat = "dd-mmm-yyyy";
var cEndTimeFormat = "h:MM tt";
cEndTime1 = AdjustMidnight(cEndTime1);
event.value = 0;
if(cStartDate != "" && cStartTime1 != "" && cEndDate != "" && cEndTime1 != "")
{
	t1 = DateTime2Min(cEndDateFormat + " " + cEndTimeFormat, cEndDate + " " + cEndTime1) - DateTime2Min(cStartDateFormat + " " + cStartTimeFormat, cStartDate + " " + cStartTime1);
}




var cStartDate = this.getField("Date").valueAsString;
var cStartTime2 = this.getField("IN_TIME2").valueAsString;
var cStartDateFormat = "dd-mmm-yyyy";
var cStartTimeFormat = "h:MM tt";
cStartTime2 = AdjustMidnight(cStartTime2);

var cEndDate = this.getField("Date").valueAsString;
var cEndTime2 = this.getField("OUT_TIME2").valueAsString;
var cEndDateFormat = "dd-mmm-yyyy";
var cEndTimeFormat = "h:MM tt";
cEndTime2 = AdjustMidnight(cEndTime2);

event.value = 0;

if(cStartDate != "" && cStartTime2 != "" && cEndDate != "" && cEndTime2 != "")
{
	t2 = DateTime2Min(cEndDateFormat + " " + cEndTimeFormat, cEndDate + " " + cEndTime2) - DateTime2Min(cStartDateFormat + " " + cStartTimeFormat, cStartDate + " " + cStartTime2);
}

event.value = t1 + t2;

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

888

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

Time Caculation - Calculated field value still remains after resetting all the fields.

Contributor ,
May 19, 2020

Copy link to clipboard

Copied

Hi,

I got this script of calculation of time 

 

on this community and i modified it to suit my form everything is working fine.. but when i reset the form .. everything should be blank or 0 but  the calculated field still keeps the last calculated value;

 

Here is the script (run through calculate tab): 

 

var cStartDate = this.getField("Date").valueAsString;
var cStartTime1 = this.getField("IN_TIME1").valueAsString;
var cStartDateFormat = "dd-mmm-yyyy";
var cStartTimeFormat = "h:MM tt";
cStartTime1 = AdjustMidnight(cStartTime1);

var cEndDate = this.getField("Date").valueAsString;
var cEndTime1 = this.getField("OUT_TIME1").valueAsString;
var cEndDateFormat = "dd-mmm-yyyy";
var cEndTimeFormat = "h:MM tt";
cEndTime1 = AdjustMidnight(cEndTime1);

event.value = 0;
if(cStartDate != "" && cStartTime1 != "" && cEndDate != "" && cEndTime1 != "")
{
var t1 = DateTime2Min(cEndDateFormat + " " + cEndTimeFormat, cEndDate + " " + cEndTime1) - DateTime2Min(cStartDateFormat + " " + cStartTimeFormat, cStartDate + " " + cStartTime1);
}


var cStartDate = this.getField("Date").valueAsString;
var cStartTime2 = this.getField("IN_TIME2").valueAsString;
var cStartDateFormat = "dd-mmm-yyyy";
var cStartTimeFormat = "h:MM tt";
cStartTime2 = AdjustMidnight(cStartTime2);

var cEndDate = this.getField("Date").valueAsString;
var cEndTime2 = this.getField("OUT_TIME2").valueAsString;
var cEndDateFormat = "dd-mmm-yyyy";
var cEndTimeFormat = "h:MM tt";
cEndTime2 = AdjustMidnight(cEndTime2);

event.value = 0;

if(cStartDate != "" && cStartTime2 != "" && cEndDate != "" && cEndTime2 != "")
{
var t2 = DateTime2Min(cEndDateFormat + " " + cEndTimeFormat, cEndDate + " " + cEndTime2) - DateTime2Min(cStartDateFormat + " " + cStartTimeFormat, cStartDate + " " + cStartTime2);
}

event.value = t1 + t2;

 

 

 

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

Try this:

var t1 = "";
var t2 = "";
var cStartDate = this.getField("Date").valueAsString;
var cStartTime1 = this.getField("IN_TIME1").valueAsString;
var cStartDateFormat = "dd-mmm-yyyy";
var cStartTimeFormat = "h:MM tt";
cStartTime1 = AdjustMidnight(cStartTime1);

var cEndDate = this.getField("Date").valueAsString;
var cEndTime1 = this.getField("OUT_TIME1").valueAsString;
var cEndDateFormat = "dd-mmm-yyyy";
var cEndTimeFormat = "h:MM tt";
cEndTime1 = AdjustMidnight(cEndTime1);
event.value = 0;
if(cStartDate != "" && cStartTime1 != "" && cEndDate != "" && cEndTime1 != "")
{
	t1 = DateTime2Min(cEndDateFormat + " " + cEndTimeFormat, cEndDate + " " + cEndTime1) - DateTime2Min(cStartDateFormat + " " + cStartTimeFormat, cStartDate + " " + cStartTime1);
}




var cStartDate = this.getField("Date").valueAsString;
var cStartTime2 = this.getField("IN_TIME2").valueAsString;
var cStartDateFormat = "dd-mmm-yyyy";
var cStartTimeFormat = "h:MM tt";
cStartTime2 = AdjustMidnight(cStartTime2);

var cEndDate = this.getField("Date").valueAsString;
var cEndTime2 = this.getField("OUT_TIME2").valueAsString;
var cEndDateFormat = "dd-mmm-yyyy";
var cEndTimeFormat = "h:MM tt";
cEndTime2 = AdjustMidnight(cEndTime2);

event.value = 0;

if(cStartDate != "" && cStartTime2 != "" && cEndDate != "" && cEndTime2 != "")
{
	t2 = DateTime2Min(cEndDateFormat + " " + cEndTimeFormat, cEndDate + " " + cEndTime2) - DateTime2Min(cStartDateFormat + " " + cStartTimeFormat, cStartDate + " " + cStartTime2);
}

event.value = t1 + t2;

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

889

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
May 19, 2020 0
Adobe Community Professional ,
May 19, 2020

Copy link to clipboard

Copied

Hi,

 

How are you resetting the form? a button, custom script.

 

Regards

 

Malcolm

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
Reply
Loading...
May 19, 2020 0
Contributor ,
May 19, 2020

Copy link to clipboard

Copied

I tried 3 methods of resetting but still the last calculated value remains... i don't want to make a RESET BUTTON to reset the field .... i want if the user deletes the value individually the calculated field resets itself..

1. RESET the form via inbuilt adobe acrobat > CLEAR FORMscreen.png

 

2. INDIVIDUALLY deleting all the values from each field.

3. Made a button  and through ACTION tab > RESET A FORM ... this also doesnot work

 

None of them resets the calculated field.

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
Reply
Loading...
May 19, 2020 0
Adobe Community Professional ,
May 19, 2020

Copy link to clipboard

Copied

Hi,

 

Are you able to share the document? as that should just work.

And which version of Acrobat are you using?

 

Regards

 

Malcolm

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
Reply
Loading...
May 19, 2020 0
Contributor ,
May 19, 2020

Copy link to clipboard

Copied

Below is the screenshot of my Adobe version

screen.png

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
Reply
Loading...
May 19, 2020 0
Contributor ,
May 19, 2020

Copy link to clipboard

Copied

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
Reply
Loading...
May 19, 2020 0
Adobe Community Professional ,
May 19, 2020

Copy link to clipboard

Copied

Have you included in your form a function called 'AdjustMidnight'?

 

Are you getting any earnings in the Acrobat JavaScript console?

 

Have you checked the field calculation order for the form?

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
Reply
Loading...
May 19, 2020 0
Contributor ,
May 19, 2020

Copy link to clipboard

Copied

yes i have included 'adjust Midnight function":

 

 

 

function AdjustMidnight(cValue)
{
	if(/^(12)(:\d{2})[ ](am)$/.test(cValue.toLowerCase()))
	{
		cValue = "00" + RegExp.$2 + RegExp.$3;
	}
	return cValue;
} 

 

 

in console window.. it give something : Type Error : f is null

 

There is only one calculated field nothing to bother about calculation order

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
Reply
Loading...
May 19, 2020 0
Contributor ,
May 19, 2020

Copy link to clipboard

Copied

Attached is the full document level script .. i got this from this community :

function Scand(cFormat, cValue)
{
	var oField = util.scand(cFormat, cValue);
	if(oField ==  null)
	{
		app.alert("Error converting date time string of " + cValue + " with a format of " + cFormat, 1, 0);
	}
	return oField;
} 



function AdjustMidnight(cValue)
{
	if(/^(12)(:\d{2})[ ](am)$/.test(cValue.toLowerCase()))
	{
		cValue = "00" + RegExp.$2 + RegExp.$3;
	}
	return cValue;
} 


function DateTime2Min(cFormat, cValue)
{
	oDate = Scand(cFormat, cValue);
	return Math.floor(oDate.getTime()/(1000*60));
} 

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
Reply
Loading...
May 19, 2020 0
Contributor ,
May 19, 2020

Copy link to clipboard

Copied

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
Reply
Loading...
May 19, 2020 0
Adobe Community Professional ,
May 19, 2020

Copy link to clipboard

Copied

Try this:

var t1 = "";
var t2 = "";
var cStartDate = this.getField("Date").valueAsString;
var cStartTime1 = this.getField("IN_TIME1").valueAsString;
var cStartDateFormat = "dd-mmm-yyyy";
var cStartTimeFormat = "h:MM tt";
cStartTime1 = AdjustMidnight(cStartTime1);

var cEndDate = this.getField("Date").valueAsString;
var cEndTime1 = this.getField("OUT_TIME1").valueAsString;
var cEndDateFormat = "dd-mmm-yyyy";
var cEndTimeFormat = "h:MM tt";
cEndTime1 = AdjustMidnight(cEndTime1);
event.value = 0;
if(cStartDate != "" && cStartTime1 != "" && cEndDate != "" && cEndTime1 != "")
{
	t1 = DateTime2Min(cEndDateFormat + " " + cEndTimeFormat, cEndDate + " " + cEndTime1) - DateTime2Min(cStartDateFormat + " " + cStartTimeFormat, cStartDate + " " + cStartTime1);
}




var cStartDate = this.getField("Date").valueAsString;
var cStartTime2 = this.getField("IN_TIME2").valueAsString;
var cStartDateFormat = "dd-mmm-yyyy";
var cStartTimeFormat = "h:MM tt";
cStartTime2 = AdjustMidnight(cStartTime2);

var cEndDate = this.getField("Date").valueAsString;
var cEndTime2 = this.getField("OUT_TIME2").valueAsString;
var cEndDateFormat = "dd-mmm-yyyy";
var cEndTimeFormat = "h:MM tt";
cEndTime2 = AdjustMidnight(cEndTime2);

event.value = 0;

if(cStartDate != "" && cStartTime2 != "" && cEndDate != "" && cEndTime2 != "")
{
	t2 = DateTime2Min(cEndDateFormat + " " + cEndTimeFormat, cEndDate + " " + cEndTime2) - DateTime2Min(cStartDateFormat + " " + cStartTimeFormat, cStartDate + " " + cStartTime2);
}

event.value = t1 + t2;

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
Reply
Loading...
May 19, 2020 2