3 Replies Latest reply on Jul 15, 2010 3:46 AM by PHANTOMIASA

    String to Date -> avoid errors

    PHANTOMIASA Level 1

      Hello!

       

      I have a utility function which converts me a String to a Date:

      For example as a stringDate: "10.12.2010 18:30:00".

       

      public static function stringToDate(stringDate:String):Date {
          var date:String = stringDate.split(" ")[1];
          var timeArray:Array = date.split(":");
          var d:Date = DateField.stringToDate(stringDate, "DD.MM.YYYY");
          d.setHours(Number(timeArray[0]), Number(timeArray[1]), Number(timeArray[2]), 0);
          return d;
      }
      

      My problem is, that a User has the possibility to enter the String in a Textfield, so it is possible that there will be an error in the string.

      And if I call the function with an invalid stringDate, I get an error.

       

      How can avoid this? I know that I have to use a DateChooser, but it is in my first release not possible.

      So how can I avoid an error message in this function and return for example a "new Date()"?

       

      Thanks a lot in advance & Best Regards.

        • 1. Re: String to Date -> avoid errors
          PHANTOMIASA Level 1

          Update: In this line I get a null error:

          var timeArray:Array = date.split(":");
          

          because my empty string parameter.

          I can check this if I do a

          if (stringDate != "")
          

          but how can I avoid the error if someone type "2010-99-99"?

           

          Do I need something like a regular expression?

           

          I created this regex:

          ^(0[1-9]|[12][0-9]|3[01]).(0[1-9]|1[012]).((19|20)[0-9]{2})\s(0[0-9]|1[0-9]|2[0-3]):([0-5] [0-9]):([0-5][0-9])$

           

          Is this regex okay for "dd.mm.yyyy hh:mm:ss"?

          And how can I check if a given string will match?

           

          Best Regards.

          • 2. Re: String to Date -> avoid errors
            BhaskerChari Level 4

            Hi

             

            You can use the try catch block in order to avoid the run time error and return new Date() if there is any invalid string and an error has occured..

             

            So your function should look like...as below:

             

            public static function stringToDate(stringDate:String):Date {
                         try
                         {
                    var date:String = stringDate.split(" ")[1];
                    var timeArray:Array = date.split(":");
                    var d:Date = DateField.stringToDate(stringDate, "DD.MM.YYYY");
                    d.setHours(Number(timeArray[0]), Number(timeArray[1]), Number(timeArray[2]), 0);
                    return d;
                         }
                         catch(error:Error)
                         {
                          return new Date();
                         }
                         return new Date();
               }

             

            Actually the error occurs at lines below in different cases...

             

            var timeArray:Array = date.split(":");
            d.setHours(Number(timeArray[0]), Number(timeArray[1]), Number(timeArray[2]), 0);

             

            So its better you put it in atry catch block so that you can avoid the error..

             

             

             

            If this post answers your question or helps, please kindly mark it as such.


            Thanks,

            Bhasker Chari


            • 3. Re: String to Date -> avoid errors
              PHANTOMIASA Level 1

              Thanks a lot, the problem is solved.

               

              Best Regards.