DateFormatter parseDateString Replacement(
February 18, 2007 at 4:36 pm | In as3, flex2 | 6 CommentsSearching 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
6 Comments »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.


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
Comment by George Varro — September 6, 2007 #
mmm extending DateFormatter
nice trick
Comment by dersteppenwolf — September 6, 2007 #
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.
Comment by Ernest — February 12, 2008 #
Thanks. Nice way to handle.
Comment by Deepak Bhatia — March 27, 2008 #
This works for me and probably allow to use more formatting options:
var date : Date = new Date(new DateFormatter().format(”01 Apr 08″));
Comment by Christian — July 9, 2008 #
[...] 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: http://flex2colombia.wordpress.com/2007/02/18/dateformatter-parsedatestring-replacement/ [...]
Pingback by Rey Nacho Lives » Blog Archive » DateFormatter parseDateString workaround — July 29, 2008 #