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

Date calculation

New Here ,
Sep 30, 2021 Sep 30, 2021

Copy link to clipboard

Copied

I would like a calculation of 2 date input which only gives gives me the year difference in whole year on the 2 inputs.
The inputs have the format [dd-mmm-yyyy] and the script shown below also seems OK for that, but I have 2 more wishes that I would like the calculation to perform.
If the inputs are only entered in the format [yyyy], it must also be able to calculate the difference throughout the year, and if one or both inputs are not of the formats [dd-mmm-yyyy] or [yyyy], the output must be blank (nothing).

Please help me with what the calculation should look like to fulfill my wishes.

 

// Custom calculate script
(function () {

var sStart = getField("Dato5").valueAsString;
var sEnd = getField("Dato6").valueAsString;
var dStart, dEnd, diff;

if(sStart && sEnd) {
dStart = util.scand("dd-mmm-yyyy", sStart);
dEnd = util.scand("dd-mmm-yyyy", sEnd);
diff = dEnd.getTime() - dStart.getTime();
event.value = Math.floor(diff / 31536e6) + " år";
} else {
event.value = "";
}

})();

TOPICS
Acrobat SDK and JavaScript

Views

242

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 ,
Sep 30, 2021 Sep 30, 2021

Copy link to clipboard

Copied

Try this:

 

 

(function () {

event.value = "";
var sStart = getField("Dato5").valueAsString;
var sEnd = getField("Dato6").valueAsString;
var dStart, dEnd, diff;

if(sStart && sEnd) {
	dStart = util.scand("dd-mmm-yyyy", sStart);
	dEnd = util.scand("dd-mmm-yyyy", sEnd);
	if (dStart && dEnd) {
		diff = dEnd.getTime() - dStart.getTime();
		event.value = Math.floor(diff / 31536e6) + " år";
	} else {
		dStart = util.scand("yyyy", sStart);
		dEnd = util.scand("yyyy", sEnd);
		if (dStart && dEnd) {
			diff = dEnd.getTime() - dStart.getTime();
			event.value = Math.floor(diff / 31536e6) + " år";
		}		
	}
}})();

 

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 ,
Sep 30, 2021 Sep 30, 2021

Copy link to clipboard

Copied

It's comming op an error message: Syntax Error: syntax error 22: at line 23

If the date inputs are not correct ([dd-mmm-yyyy] or [yyyy]) will it come up with a blank field (output)?

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 ,
Sep 30, 2021 Sep 30, 2021

Copy link to clipboard

Copied

Fixed it above now.

And yes, the field will be blank if you enter something in a different format.

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 ,
Sep 30, 2021 Sep 30, 2021

Copy link to clipboard

Copied

LATEST

It works - thanks for the help.

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