I'm searching for:
   
   

Richard
"No alarms and no surprises please"

My Blog



Working with eBay's auction EndDate in Flash
July 27, 2007

Flash is an excellent tool and it's been a lot of fun rediscovering it while working on the widgets.  However, there are just a few things that it's lacking that can be somewhat of pain while developing inside it.  9 times out of 10 you can usually find a work around or if you look hard enough you can find a buried article from someone who came across the same problem as you and came up with a work around.

One problem I came across during the widget development was parsing out the EndDate value that eBay webservices uses to denote when the auction you are looking at is going to end.  eBay uses an UTC formated date value that looks like this:  "2008-07-16T05:30:45.000Z"

I was relieved to find that Flash 8 supports the UTC Date type, but then realized that I could not find a way to take the string value of the EndDate that I got from eBay and turn it into a Flash formatted UTC date.  Hmmrph.  C#, as well as a good number of other server technologies make this quite simple so  I searched the innernets and came up empty for an example of what I was looking for.  Looking at Flash I discovered that I could construct a valid UTC Date with the following format: 

var myDate = new Date(year,month,day,hour,min,secs,ms);

Looking at the EndDate value of the eBay record I saw that I had all the values that I needed to construct a valid UTC Date, but I was going to have to roll up my sleeves and chop the string value up a little bit to get what I wanted.

Note:  Flash Months are zero based, unlike most languages, so January is equal to 0 instead of 1. 

So here is the function I created that will take a string that contains a date and convert it to proper Date.UTC format in flash.  It simply hacks apart the string and grabs the valid numbers that we need to pass to the constructor.  To expand on this, you could cast the variables as Numbers and do some extra validating before passing it to the constructor.  This could also probably be a good example for conversion to a prototype function.

function ConvertStringToUTC(dt:String):Date {
 var year = dt.substring(0,4);
 var month = dt.substring(5,7);
 var day = dt.substring(8,10);
 var hour = dt.substring(11,13);
 var min = dt.substring(14,16);
 var secs = dt.substring(17,19);
 var ms = dt.substring(20,23);
 
 var myDate = new Date(year,(month-1),day,hour,min,secs,ms);
 return myDate;
}

usage would be:

Date dtEndDate = ConvertStringToUTC("2008-07-16T05:30:45.000Z");

Views: 27

Comments

No comments have been added to this blog. Why not be the first?