DateFormatter parseDateString Replacement(

February 18, 2007 at 4:36 pm | Posted in as3, flex2 | 8 Comments

Searching in flex documentation I have found that parseDateString is a protected method… (why protected? it really sucks…)

If you need a “quick and dirty” method to parse a date from a string, maybe this could help you:

public class DateUtil {

public static function parseString(stringDate:String) : Date{
var milis:Number = Date.parse(stringDate);
return new Date(milis);
}
}

stringDate Format restrictions (From Date Documentation):

The Time Zone Designation (TZD) is always in the form GMT-HHMM or UTC-HHMM indicating the hour and minute offset relative to Greenwich Mean Time (GMT), which is now also called universal time (UTC). The year month and day terms can be separated by a forward slash (/) or by spaces, but never by a dash (-). Other supported formats include the following (you can include partial representations of these formats; that is, just the month, day, and year):

     MM/DD/YYYY HH:MM:SS TZD

     HH:MM:SS TZD Day Mon/DD/YYYY

     Mon DD YYYY HH:MM:SS TZD

     Day Mon DD HH:MM:SS TZD YYYY

     Day DD Mon HH:MM:SS TZD YYYY

     Mon/DD/YYYY HH:MM:SS TZD

     YYYY/MM/DD HH:MM:SS TZD

8 Comments »

RSS feed for comments on this post. TrackBack URI

  1. If the parseDateString method is protected, make it to “public” with a little trick!

    Make a new class with a public static method and in it you can invoke the parseDateString and it will return with a Date object!

    package utils
    {
    import mx.formatters.DateFormatter;
    public class DateUtil extends DateFormatter
    {
    public static function parseDate(str:String):Date {
    return parseDateString(str);
    }
    }
    }

    Notice:
    And with this class the “but never by a dash (-)” limitation is gone forever!

    regards,
    George

  2. mmm extending DateFormatter
    nice trick😛

  3. Class mx.controls.DateField has a static function with signature

    stringToDate(valueString:String,inputFormat:String):Date

    It does not read time information though, but it works for me.

  4. Thanks. Nice way to handle.

  5. This works for me and probably allow to use more formatting options:

    var date : Date = new Date(new DateFormatter().format(“01 Apr 08”));

    • Finally a solution that worked for me!🙂
      Thanks a lot!!!!🙂🙂🙂🙂

  6. […] I ran into a situation recently which required me to parse a string into a Date object in Flex. After some searching I found that there’s a wonderful little method in the DateFormatter class called parseDateString. Unfortunately, that method is protected and I couldn’t gain access to it. Luckily, I came accross a blog post that provided a wonderful solution: https://flex2colombia.wordpress.com/2007/02/18/dateformatter-parsedatestring-replacement/ […]

  7. Can this be used to localize the date? I have mt resource bundle setup to get the translations for Spanish, but not sure how to get the day/month through a date formatter.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: