Flex - Convert a SQLServer Datetime into ActionScript Date
This article is from the original REM DEV blog (2008–2010). Some of the technologies it covers (Flex, Flash, AIR, JavaFX 1.x) have since been retired.
In my Appointments table, I have a field called apptStartTime, which stored in sqlserver as a datetime. I had to figure out how to fetch the date in a format that the ActionScript Date object could use. So in my sql statement, I had to convert the date like this:
select
convert(varchar(20),apptStartTime,109) + ' ' + right(convert(varchar(26),apptStartTime,109),2)
from Appointments
In my application I was actually pulling the data in xml format and the apptStartTime was an attribute of a 'data' element.
Once the data reached the client, I would have thought that I could just pass the value from the xml attribute into the Date constructor like this...
var myDate:Date = new Date(data.@apptStartTime);
BUT THIS DIDN'T WORK!
So I created a function to parse the date, like this:
private function parseDate(str:String):Date{
try{
return new Date(str);
}catch(e:Error){
}
return null;
}
So passing my data from the xml into the parseData() function did the trick:
var myDate:Date = parseDate(data.@apptStartTime);