2 Replies Latest reply on Dec 19, 2007 1:38 PM by Peter deHaan

    How can I convert a String to a Date?

    Cacademoniaca Level 1
      My string has the following format DD/MM/YYYY (spanish standard)

      var date: Date = new Date('01/02/2001') produces January the second, insted of Febraury the first.

      Any one know if there's a class like java SimpleDateFormat that has a parse method.

      Something that would take a String an output the get... I know it's not a big deal to make my own but I rather used a tested common lib!

      Thanks :D
        • 1. Re: How can I convert a String to a Date?
          Sreenivas R Adobe Employee
          You should be able to use Date.parse method.

          new Date(Date.parse("01/02/2001"));
          • 2. Re: How can I convert a String to a Date?
            Peter deHaan Level 4

            You can try using the static mx.controls.DateField.stringToDate() method, which allows you to specify a date mask.

            Something like this:
            var dateString:String = "01/02/2001";
            var dateFormat:String = "DD/MM/YYYY";

            var date1:Date = new Date(dateString);
            var date2:Date = new Date(Date.parse(dateString));
            var date3:Date = DateField.stringToDate(dateString, dateFormat);

            trace("1:", date1);
            trace("2:", date2);
            trace("3:", date3);

            1: Tue Jan 2 00:00:00 GMT-0800 2001
            2: Tue Jan 2 00:00:00 GMT-0800 2001
            3: Thu Feb 1 00:00:00 GMT-0800 2001

            Hope that helps,