Skip to main content
vanfrei
Participant
January 2, 2018
Question

Separarte a date in day,month,year

  • January 2, 2018
  • 3 replies
  • 2530 views

I have a Field with the name "date-1" an three fields with the name "day-1" "month-1" and "year-1". I need a Formula (Javascript?) with wich I can separate the day, month and year from the date ("date-1") so I can found them in the fields "day-1" "month-1" and "year-1".

Thanking you in advance

Regards

Claus

This topic has been closed for replies.

3 replies

Inspiring
January 2, 2018

The most general approach would be to use the util.scand and util.printd methods to manipulate the input date string. This approach woul be the easiest to maintain since on would only need to adjust the format of the input date string for any changes in the date format.

// on blur action for the input date;

// clear result fields;

     this.getField("day-1").value = "";

     this.getField("mmm-1").value = "";

     this.getField("year-1").value = "";

// process if entry not empty;

if(event.value 1= "")

{

     var cFormat = "mm/dd/yyyy"; // change to match the input date format;

     // convert input date to JavaScript date format;

     var oDate = util.scand(cFormat, event.value);

     // set the values of the individual date components;

     this.getField("day-1").value = util.printd("dd", oDate); // change display format as needed;

     this.getField("mmm-1").value = util.printd("mm", oDate); // change display format as needed;

     this.getField("year-1").value = util.printd("yyyy", oDate); // change display format as needed;

} // end field not empty;

Thom Parker
Community Expert
Community Expert
January 2, 2018

If the dates use a consistent format, then all you have to do is split them based on the date separator:

Here's an article on splitting strings.

https://acrobatusers.com/tutorials/splitting-and-rebuilding-strings

For Example, if the date is 2018-1-2 then this code splits it up

var aDateParts = this.getField("date-1").value.split("-");

But if the date is more complicated, such as "Dec 3, 2017", it'll need to be split up a bit differently.

Here are some articles on dates:

https://acrobatusers.com/tutorials/date_time_part1

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-2

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
January 2, 2018

What is the format of the value in "date-1"?