Copy link to clipboard
Copied
Is it possible to have the date entered to show in three different fields? For example, if I enter 3/13/2016 I would like to show it like this, Third month, thirteenth day, Year of 2016. In three separate fields. Can someone help with a script to do it? Thanks guys.
I completely understand, programming is hard, and it requires a different way of thinking.
You are almost there. It may work, but there are two issues I see that might bite you later.
The first one is that you should "declare" the "theMonth" variable (which was in my original script, but you probably removed it when you added the array of month names. You are already declaring (and initializing) the variables "theDay" and "theYear", so just add this line to these two:
var theMonth = "";
And, then a
...Copy link to clipboard
Copied
Do you want that same string to appear in 3 different fields or do you have a field for the month, date and year you want populated.
Do you have any scripting or coding experience?
Acrobat JavaScript and JavaScript do not have any function for ordinal numbering and does not have a number to words function, so this will require some additional coding to get the ordinal month number from the date string as well as converting numbers to words.
Copy link to clipboard
Copied
I have three text fields. One called Month, one called Day, on called Year. The date field I'd like to enter the date formatted m,dd,yyyy. It doesn't need to be converted into the wording because that's already set in the text when I created the doc in InDesign. I would just like to split up the date and have the numbers go into the text fields.
The way the fields appear in the doc is like this:
Day_______, Month_______, Year_______. Then I have txtFields in place where the blanks are.
As far as coding. I have a script to automatically populate the formatted date field. It's a real short script I place in the doc level. My coding experience is very novice at best. Like most forum participants here. But, I can take instructions real well and I'm very good at learning from examples. This bit of scripting shouldn't be all that complicated and is why I'm asking for a bit of help.
Copy link to clipboard
Copied
Here is some background about how the JavaScript engine in Acrobat works:
There are two parts to each JavaScript implementation: The core language, which is the same for all JavaScript environments, and the application specific extensions (which as the name implies are in this case specific to Acrobat). The good news is that the core JavaScript language has a "Date" object, which allows you to convert from and to dates, and extract data from such a date object (like e.g. the day, month and year that make up the Date object). See here for more information about what you can do with a Date object:
To convert from a string to a Date object, you can either use the functionality built into the Date object, or a method that Acrobat provides (which in general works better):
Acrobat DC SDK Documentation - util.scand
Once you combine all that togther, you very likely come up with something like this:
var theMonth = "";
var theDay = "";
var theYear = "";
// get the information from the "theDate" field
var theDate = this.getField("theDate").valueAsString;
if (theDate != "") {
// convert the date string to a JavaScript Date object
var d = util.scand("m,dd,yyyy", theDate);
if (d != null) {
theMonth = d.getMonth();
theDay = d.getDate();
theYear = d.getFullYear();
}
}
event.value = theMonth;
this.getField("Day").value = theDay;
this.getField("Year").value = theYear;
This assumes that you have four fields, one is called "theDate", which contains your date in the somehow strange format m,dd,yyyy - bt you can change the format by changing the first parameter of util.scand(), and three fields called Month, Day and Year. The script would be used as the custom calculation script for the "Month" field.
Copy link to clipboard
Copied
Thank you once again Mr Kremer for your help. The script you have provided works right on except there's something a little quirky with the Month field. When I type in a date in the "theDate" field the month shows in the Month field 1 behind. Kinda like its subtracting 1 from whatever date is entered into theDate field. The Day and Year fields functions perfectly it's just the Month field that I don't understand. I don't know what to check or change to get the Month to cooperate and show the correct month.
The only thing I tried to change was the formatting, first to mm,dd,yyyy then to mmmm,dd,yyyy but the Month field was still 1 behind the date in theDate field.
I've gone thru the links you provided and could not gleam the information I need. Could you shed some light and guide me in the right direction as to what (and why this is happening). Everything works except the Month field.
Copy link to clipboard
Copied
Month is zero based, so January is 0. You need to add onebor use the util.printd () method with a format of "m".
Copy link to clipboard
Copied
var theMonth = "";
var theDay = "";
var theYear = "";
// get the information from the "theDate" field
var theDate = this.getField("theDate").valueAsString;
if (theDate != "") {
// convert the date string to a JavaScript Date object
var d = util.printd("m,dd,yyyy", theDate);
if (d != null) {
theMonth = d.getMonth();
theDay = d.getDate();
theYear = d.getFullYear();
}
}
event.value = theMonth;
this.getField("Day").value = theDay;
this.getField("Year").value = theYear;
Was this what you were referring to? I've tried the script this way and it didn't work at all. Where else can I change it?
Copy link to clipboard
Copied
No. util.printd uses the JavaScript date object not the date string. Did you look at the documentation?
Copy link to clipboard
Copied
Is it possible to have the Month to show the month name instead of the number? What would I have to change? I tried to change the format, it didn't do anything to help.
Copy link to clipboard
Copied
Sorry about the offset problem. That's what happens when you just type code in the forum editor and not actually try it. But, that's also why it's never a good idea to just copy code you find online and use it without understanding what it does. You need to be able to fix errors in the code - or adjust it to changing requirements down the road. That's why I link to documentation so that you can understand what's done.
To get the correct number for the month, change the line that assigns 'theMonth' as follows:
theMonth = d.getMonth() + 1;
If you want the name of the month, don't make this change, but make a different change. One reason why you are getting a 0 based month value is that it's very easy to use that as an index in an array of names, and then use a specific month name instead of the number. To do that, you will need to read up on JavaScript arrays:
You start out with creating an array variable that contains all you month names (do this at the beginning of the script):
var monthNames = [ "January", "February", "March", "April", "May", /* just fill in the other months */ ];
In the line where you assign the value to the 'theMonth' variable, use this instead:
theMonth = monthNames[d.getMonth()];
If you want to learn more about JavaScript for Acrobat, consider this resource (you will have to pay for the book): Beginning JavaScript for Adobe Acrobat
Copy link to clipboard
Copied
Mr Kremer,
I believe you taught me something today regarding scripting. I have been trying to learn more and more everyday. The thing is about reading books is understanding and relating what I'm reading to what I'm trying to do. I find it so daunting and confusing. I read all the links and references I receive, but at times it makes me overwhelmed. But, I stick with it with the hopes of something getting into the brain pan. And, at times it does. This is what got thru.
This is the script I came up with, with your help and guidance. To my own surprise, I got it. This works: Did I get it correct?
var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var theDay = "";
var theYear = "";
// get the information from the "theDate" field
var theDate = this.getField("theDate").valueAsString;
if (theDate != "") {
// convert the date string to a JavaScript Date object
var d = util.scand("mmmm,dd,yyyy", theDate);
if (d != null) {
theMonth = monthNames[d.getMonth()];
theDay = d.getDate();
theYear = d.getFullYear();
}
}
event.value = theMonth;
this.getField("Day").value = theDay;
this.getField("Year").value = theYear;
this.getField("Month").value = theMonth;
Thank you for the invaluable time you spend helping people and myself. I really appreciate it.
Copy link to clipboard
Copied
I completely understand, programming is hard, and it requires a different way of thinking.
You are almost there. It may work, but there are two issues I see that might bite you later.
The first one is that you should "declare" the "theMonth" variable (which was in my original script, but you probably removed it when you added the array of month names. You are already declaring (and initializing) the variables "theDay" and "theYear", so just add this line to these two:
var theMonth = "";
And, then at the end you can remove one line - this is the calculation script for the "theMonth" field, which means that we do not have to set the value of the field via 'this.getField("theMonth").value' - because it is a calculation script, you set the value of the calculated field using the 'event.value' property. So, remove this line from the end of your script:
this.getField("Month").value = theMonth;
That should do it.
Copy link to clipboard
Copied
Yes, as per the Acrobat JavaScript API, one can use the format string "mmmm" in the util.printd object.
If you are starting out with a field with a date string like "1/1/1970" or "Jan 1, 1970", you must first convert that text string to an object know as the date object either by parsing the date string into month, date, and year and then using those 3 values to create a date object. Acrobat JavaScript provides the "util.scand" to convert a date string into a date object using two parameters, the visual string for the date construct and the date string. This function will also validate the date or make sure the provided date string is a valid date.
See MDN JavaScript Date object for more information about the JavaScript date object. See Adobe Acrobat JavaScript API Reference util.sand for more information about this function/method.
Once one has the date object on can get various parts of the data that make up a date or date and time. These items could be the year, month, date, time zone offset at the users location, or various UTC date and time components or perform computation on the various items that make up the date object. Acrobat not only provides a tool to convert a date string into a date object but also format a date object to display the date and time in many character strings, this is documented in the Acrobat JS API Reference under the util.printd() method or function.
So if I have a date string of "01/31/1970" with a format of "mm/dd/yyyy" I can get the parts of the date with the following script:
var cDate = "01/31/1970"; // get the date string formatted as "mm/dd/yyyy";
var oDate = util.scand("mm/dd/yyyy", cDate); // convert date string to date object;
// get the parts of the date;
var nFullYear = oDate.getFullYear(); // get four digit year;
var nMonth = oDate.getMonth(); // get the zero based month;
var nDate = oDate.getDate(); // get the date of the month;
var nDay = oDate.getDay(); // get the zero based day of the week;
// display the data:
app.alert("For the date \"" + cDate + "\":" +
"\nFull year: " + nFullYear +
"\nZero based month number: " + nMonth +
"\nDate of month: " + nDate +
"\nZero based day of the week: " + nDay +
"\nTime Zone Offset in minutes: " + oDate.getTimezoneOffset(), 1, 0);
// display the data using util.printd;
app.alert("Formatting using the \"util.printd\" method:" +
"\nFull year: " + util.printd("yyyy", oDate) +
"\nLong month: " + util.printd("mmmm", oDate) +
"\nShort month: " + util.printd("mmm", oDate) +
"\n2 digit month: " + util.printd("mm", oDate) +
"\n1 digit month: " + util.printd("m", oDate) +
"\n2 digit date: " + util.printd("dd", oDate) +
"1\n1 digit date: " + util.printd("d", oDate) +
"\nLong day of the week: " + util.printd("dddd", oDate) +
"\nShort day of the week" + util.printd("ddd", oDate), 1, 0);