4 Replies Latest reply on Feb 18, 2009 8:52 PM by Madhav Subedi

    Parsing a formatted date string to a date object

    cicero111
      Is it possible to simply parse a date string back to a date object which is formatted with a date formatter. I don't want to write an own parser.


      var df:DateFormatter = new DateFormatter();
      df.FormatString ="DD.MM.YYYY";
      var myCurrentDate:Date = new Date();
      myCurrentDateString:String = df.Format(myCurrentDate); // e.g "17.02.2009"

      var myNewDate:Date = xxx.xxx(myCurrentDateString);

      Some ideas for xxx.xxx?

      I hope there is something simple like
      myNewDate:Date = df.parse(myCurrentDateString);

      Date.parse() only seems to parse a non-formatted date string.

        • 1. Re: Parsing a formatted date string to a date object
          Level 7

          "cicero111" <webforumsuser@macromedia.com> wrote in message
          news:gnee3v$7lq$1@forums.macromedia.com...
          > Is it possible to simply parse a date string back to a date object which
          > is
          > formatted with a date formatter. I don't want to write an own parser.
          >
          >
          > var df:DateFormatter = new DateFormatter();
          > df.FormatString ="DD.MM.YYYY";
          > var myCurrentDate:Date = new Date();
          > myCurrentDateString:String = df.Format(myCurrentDate); // e.g "17.02.2009"
          >
          > var myNewDate:Date = xxx.xxx(myCurrentDateString);
          >
          > Some ideas for xxx.xxx?
          >
          > I hope there is something simple like
          > myNewDate:Date = df.parse(myCurrentDateString);
          >
          > Date.parse() only seems to parse a non-formatted date string.

          I think DateField has a function that does this. You may want to go through
          its API reference in detail.


          • 2. Re: Parsing a formatted date string to a date object
            rtalton Level 4
            Trying to convert a string like "17.02.2009" fails on my system because Date() is using my local date settings (like: 02/17/2009).
            So you need to change the formatter's formatString to match the local settings, then use THAT string to convert to a Date() object.

            Here are some examples to help you; play around and you'll see what's happening:

            • 3. Re: Parsing a formatted date string to a date object
              cicero111 Level 1
              It work's fine with DateField, but in ActionScript I found nothing appropriate.
              • 4. Re: Parsing a formatted date string to a date object
                Madhav Subedi Level 4
                I didnot get your statement - "It work's fine with DateField, but in ActionScript I found nothing appropriate."

                try once using following:
                public static function stringToDate(valueString:String, inputFormat:String):Date
                var dob:Date = DateField.stringToDate("06/30/2005", "MM/DD/YYYY");

                public static function dateToString(value:Date, outputFormat:String):String
                var todaysDate:String = DateField.dateToString(new Date(), "MM/DD/YYYY");

                DateField is available in the package mx.controls
                and as these functions are static, you can use them directly.
                so,
                xxx.xxx -- DateField.stringToDate

                hope it is useful.