4 Replies Latest reply on Mar 17, 2017 7:04 AM by Karl Heinz Kremer

    Jahreszahl aus Datum extrahieren // Extract Year from Date field

    heikew62753019 Level 1

      Hallo,

       

      ich möchte gern in einem Textfeld ein Datum nach dem Schema "ddmmyyyy" eingeben und anschließend nur mit der Jahreszahl im javascript weiter arbeiten.

      Kann mir hier jemand weiter helfen? Irgendwas scheine ich zu übersehen dabei.

      Mein Textfeld ist aktuell auf Datum mit der entspr. Syntax eingestellt.

       

      //

       

      I would like to enter a date according to the schema "ddmmyyyy" in a text field and then only work with the year number in javascript.

      Is anyone able to help me? Something I seem to overlook.

      My text field is currently set to date with the corresponding syntax.

        • 1. Re: Jahreszahl aus Datum extrahieren // Extract Year
          Cari Jansen Adobe Community Professional

          Disclaimer: I'm NOT at all a JavaScripting expert ;-) so other will likely have much more efficient ways to do this. I'm hoping they will contribute too

           

          Basically you'd need to convert the date to a string that you can break down into different elements.

           

          If the date was entered as dd/mm/yyyy that is relatively easy as you have a clear delimiter you can use to split the string and push the different sections into an array.

           

          You'd get something like his.

           

          var d = getField("myDate").value; // pulls the date entered from a field named 'myDate' into variable d
          var myDateSplit = d.split("/");   // breaks date string into different pars and pushes these into an array
                                            // with  [0]= dd, [1]=mm, and [2] = yyyy
          var y = myDateSplit[2];           // assigns the yyyy value from the array to variable y
          this.getField("myYear").value = y;  // you can enter this value in a field named 'myYear'
          

           

          I know it's not a direct answer to your question, but will hopefully help with a step in the right direction.

          • 2. Re: Jahreszahl aus Datum extrahieren // Extract Year
            Cari Jansen Adobe Community Professional

            I puzzled on this a bit more and found you can also do this, which will work with your selected date format:

             

            var d = getField("myDate").value;
            var s = d.toString();  // add this step to convert what is otherwise seen as a number to a string
            
            var myArray = s.split("/")  
            
            var y = myArray[2];
            
            this.getField("myYear").value = y;
            
            • 3. Re: Jahreszahl aus Datum extrahieren // Extract Year from Date field
              Cari Jansen Adobe Community Professional

              I've moved this post to JavaScript subforum for Acrobat.

              • 4. Re: Jahreszahl aus Datum extrahieren // Extract Year from Date field
                Karl Heinz Kremer Adobe Community Professional

                There are different ways you can approach this. The most important aspect here is that JavaScript does not know what you have in your date field: It looks like a number, so JavaScript will actually interpret whatever is entered as a number, which means that it will remove leading zeros. This means that you cannot just use Field.value, you need to use Field.valueAsString to get the correct - unmodified - field contents.

                 

                Once you have the contents of the field, you can use String.subString() method (String.prototype.substring() - JavaScript | MDN) to extract the year information from your string. You can use a regular expression as well.

                 

                However, in my opinion, the right way to do this would be to convert the string to a JavaScript date and then get the year from that date object:

                 

                var d_str = this.getField("Text1").valueAsString;
                if (d_str != "") {
                    var d = util.scand("ddmmyyyy", d_str);
                    if (d != null) {
                        event.value = d.getFullYear();
                    }
                }
                else {
                    event.value = "";
                }