4 Replies Latest reply on May 26, 2014 1:17 PM by MatLac

    Date format

    MatLac Level 1

      My goal is to let the user input time as he pleases but then force an hour format of HH:MM.  I believe the best way to acheive this is use a custom keystroke allowing only numbers with a regex ( since the number format won't allow a leading "0" or a ":") and then use a blur script to take the value and turn it to a Date object of the desired format.  I can't get to turn my string into a Date Time object.

       

      value is 0545 (for 05:45)

       

      if (event.target.value != ""){

      var cDate = event.target.valueAsString;

      console.println(cDate)

      var xDate = util.scand("HHMM", cDate)

      console.println(xDate)

      }

      result is:

      Sat Jan 01 2000 00:00:00 GMT-0500 (Est)

       

      I read through the JS object reference, also this tutorial from Thom Parker, W3, google.....can't find how to take a string from a field and turn it to time.

        • 1. Re: Date format
          try67 MVP & Adobe Community Professional

          You can't use only the time to create a Date object using scand. It's simply not enough information.

          However, what do you need scand for? All you really want to do, it seems, is stick a colon in the middle, no? For that you don't need a Date object.

          • 2. Re: Date format
            MatLac Level 1

            Well, I was hoping turning it to a Date object would at the same time validate its format without having to script a complexe regex for time.  I would have rejected a null object.

            The time format from format tab doesn't allow HHMM, only HH:MM.

             

            But basically, yes, I just want to force a colon while the user will enter only numbers.  Should I use a combination of keystroke for number and regex for validation or is there a simpler way?

            • 3. Re: Date format
              try67 MVP & Adobe Community Professional

              I've developed for you the following scripts for validating and formatting this value. It's also possible to use a custom keystroke script to only allow numbers to be entered to the field.

               

              Custom Format script:

              if (event.value)

                  event.value = event.value.substring(0,2) + ":" + event.value.substring(2);

               

              Custom Validation script:

              if (event.value) {

                  if (/^\d{4}$/.test(event.value)==false) {

                      app.alert("Invalid time value.");

                      event.rc = false;

                  } else {

                      var d = util.scand("mm-dd-yyyy HH:MM", "01-01-2014 " + event.value.substring(0,2) + ":" + event.value.substring(2));

                      if (d==null) {

                          app.alert("Invalid time value.");

                          event.rc = false;

                      } else {

                          event.rc = true;

                      }

                  }

              }

              • 4. Re: Date format
                MatLac Level 1

                works like a charm and so much simpler than what I expected