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

Filling a text field with a month name taken from a date field

Community Beginner ,
Jan 10, 2023 Jan 10, 2023

I created a table filled with some date fields, starting, let's say, from day=0 to day=n

[using for each value from day=1 to day=n the script:

var theDate = util.scand("dd/mm/yyyy", this.getField("Day 0").value); theDate.setDate(theDate.getDate()+n);

event.value = util.printd("dd/mm/yyyy", theDate);]

 

Now, I want to create a new text field (as table title) showing, for instance, "January 2023" if both day=0 and day=n are in the same month (for example from Jan-12 to Jan-25) BUT it must show "January-February 2023" if day=0 and day=n are in two months in sequence (for example from Jan-22 to Feb-4) OR "December 2023-January 2024" if the date span is for example from Dec-22 to Jan-4.

 

Thank you in advance

TOPICS
Create PDFs , JavaScript , PDF forms
1.5K
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 ,
Jan 10, 2023 Jan 10, 2023

Use "mmmm" to get month in text, something like this:

util.printd("mmmm", theDate)

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 ,
Jan 10, 2023 Jan 10, 2023

You can use this code to do it:

 

var s1 = this.getField("Day 0").valueAsString;
var s2 = this.getField("Day 1").valueAsString;
if (s1=="" || s2=="") event.value = "";
else {
	var d1 = util.scand("dd/mm/yyyy", s1); 
	var d2 = util.scand("dd/mm/yyyy", s2); 
	if (d1.getMonth()==d2.getMonth() && d1.getFullYear()==d2.getFullYear()) {
		event.value = util.printd("mmmm yyyy", d1);
	} else {
		if (d1.getFullYear()==d2.getFullYear()) {
			event.value = util.printd("mmmm", d1) + "-" + util.printd("mmmm", d2) + " " + util.printd("yyyy", d1);
		} else {
			event.value = util.printd("mmmm yyyy", d1) + "-" + util.printd("mmmm yyyy", d2);
		}
	}
}
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 ,
Jan 12, 2023 Jan 12, 2023

The script works very fine, thank you so much! It shows month names in italian, as set in my Mac OS.

I wonder if it would be possible to get month names in all capital letters (as "GENNAIO" instead of "Gennaio").

Thank you again for your precious help! 

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 ,
Jan 12, 2023 Jan 12, 2023

Sure, just change these parts of the code:

util.printd("mmmm", d1)

To:

util.printd("mmmm", d1).toUpperCase()

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 ,
Jan 13, 2023 Jan 13, 2023
LATEST

Done. Thank you again.

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