Reputation: 15
I am taking date input from user in text field, which is in format DD/MM/YYYY. How to convert this string to date object in Flex. Platform: Adobe Flash Builder 4.6
Upvotes: 1
Views: 6233
Reputation: 58
If you are not on the latest Apache SDK (I know we aren't because of third party components) you basically have to write your own conversion.
The built in DateFormatter has the static method, parseDateString, but you have no way of specifying the format of the string. It was a bit rubbish!
If you definitely have no localisation issues and are sure the date is ALWAYS in DD/MM/YYYY format you could use the following:
public function stringToDate(date:String):Date {
// Extract year, month and day from passed in date string
var year:int = IntFromSubString(date, 6, 4);
var month:int = IntFromSubString(date, 3, 2);
var day:int = IntFromSubString(date, 0, 2);
// Always remember Flex months start from 0 (Jan=0, Feb=1 etc.) so take 1 off the parsed month
return new Date(year, month-1, day);
}
private static function IntFromSubString(date:String, start:int, length:int):int {
return parseInt(date.substr(start, length)) as int;
}
Upvotes: 0
Reputation: 13327
Since Flex SDK 4.10.0 you can use
DateFormatter.parseDateString(s, "DD/MM/YYYY");
Former versions of parseDateString
didn't respect a format string, so it cannot parse dateString value formatted with non default en_US format
Upvotes: 3
Reputation: 8403
Use DateField's stringToDate method. DateFormatter also has a parseDateString
function but for some reason it's set to protected.
public function convertStringToDate(s:String):Date
{
return DateField.stringToDate(s, "DD/MM/YYYY");
}
Upvotes: 0