Trying to parse dates from strings in AS3 is not always straight forward.
From today's lesson we learn that the Date.parse method returns a Number, not and int, and this number can be used to create a Date object using the Date() constructor. However, the Date may not contain any dash "-", which is commonly used in some local Date formats, since that would produce "Invalid Date".
This is how it's done:
function convertDate(value:String):Date
{
return new Date(Date.parse(value));
}
trace(convertDate("2011-10-27 15:12:00")); // OUTPUT: Invalid Date
trace(convertDate("2011 10 27 15:12:00")); // OUTPUT: Invalid Date
trace(convertDate("2011/10/27 15:12:00")); // OUTPUT: Thu Oct 27 15:12:00 GMT+0200 2011
If you receive string values using dashes "-", this can easily be fixed by replacing them with "/".
value = value.split("-").join("/");
Another pitfall is to use an int instead of a Number:
var date:Date = new Date();
var intValue:int = Date.parse(date); // intValue = 1166693128 (incorrect)
var numValue:Number = Date.parse(date); // numValue = 1319721653000 (correct)
For more information about accepted date formats and generally about the Date object, see:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Date.html#parse%28%29
For character replacement use:
ReplyDeletevalue = value.replace("-", "/");