Hey Everyone,

I am working on a program guide that passes dates from SQL Server 2005 to a .swf and back via a web service. Therefore I needed a way to transform the DateTime date column value to a Date in Actionscript.

I have no idea if there are libraries or tools in existence for doing this, so I just created my own DateTransformFactory with static methods for handling the transforms from one date type to another.

The DateTime SQL column value is coming over as a string, so I am just processing it and creating a date object. To send back, I am breaking down the date object and formatting it to a string. SQL Server on the other end will accept this string on a column set as datatype 'DateTime' and convert it accordingly.

So here is the class. If anyone has any suggestions for improvements, they would be greatly appreciated. Also if you want to add to the class - say for use with MySql date columns, or similar, please feel free to modify and post.

I figured most of us will eventually need date processing for one reason or another, so I figured I'd post a quick solution free to use for everyone.

Enjoy!

PHP Code:
package  
{
    
// SQL DateTime column value example: 2009-05-04T14:00:00
     
    
public class DateTransformFactory 
    
{
        
        public function 
DateTransformFactory() 
        {
            
        }
        
        
// convert SQL DateTime string to AS3 Date object
        
public static function convertSQLDateTimeToASDate(s:String):Date
        
{
            
// split the date from the time
            
var dt:Array = s.split("T");
            
// separate the date values
            
var d:Array = dt[0].split("-");
            
// separate the time values
            
var t:Array = dt[1].split(":");
            
            
// do if statement here
            
d[0] = adjustYearForPrevMilennia(d[0]);
            var 
date:Date = new Date(d[0], d[1]-1d[2], t[0], t[1], t[2]);
            return 
date;
        }
        
        
// helper function to enforce 1900 series year values
        
private static function adjustYearForPrevMilennia(year:int):int 
        
{
            if (
year 2000)
            {
                var 
syear:String year.toString();
                
syear syear.substr(22);
                
year parseInt(syear);
            }
            
            return 
year;
        }
        
        
// convert AS3 date to SQL DateTime formatted string
        
public static function convertASDateToSQLDateTime(date:Date):String 
        
{
            
// month processing
            
date.month++;
            var 
month:String enforceLeadingZero(date.month);
            
            
// day processing
            
var day:String enforceLeadingZero(date.day);
            
            
// hour processing
            
var hours:String enforceLeadingZero(date.hours);
            
            
// minutes processing 
            
var minutes:String enforceLeadingZero(date.minutes);
            
            
// seconds processing 
            
var seconds:String enforceLeadingZero(date.seconds);
            
            return 
date.fullYear "-" month "-" day "T" hours ":" minutes ":" seconds;
        }
        
        
// same as above using UTC
        
public static function convertASDateUTCToSQLDateTime(date:Date):String 
        
{
            
// month processing
            
date.month++;
            var 
month:String enforceLeadingZero(date.monthUTC);
            
            
// day processing
            
var day:String enforceLeadingZero(date.dayUTC);
            
            
// hour processing
            
var hours:String enforceLeadingZero(date.hoursUTC);
            
            
// minutes processing 
            
var minutes:String enforceLeadingZero(date.minutesUTC);
            
            
// seconds processing 
            
var seconds:String enforceLeadingZero(date.secondsUTC);
            
            return 
date.fullYearUTC "-" month "-" day "T" hours ":" minutes ":" seconds;
        }
        
        
// leading zero helper function needed for correct fromatting of SQL ready string
        
private static function enforceLeadingZero(i:int):String
        
{
            if (
i.toString().length 2)
            {
                return 
"0" i;
            }
            else 
            {
                return 
i.toString();
            }
        }
        
    }