Sunday, March 15, 2009

Getting The Greenwich Mean Time (GMT)

Someone asked this on a Flash BBS:
"How do I get the Greenwich mean time instead of the local time on the client's PC?"

My first thought was to find a web page that gives the current GMT.

And I found this!

The rest is rather simple.
Just load this page using the URLLoader class and do whatever you can come up with to splice out the GMT information.

My approach is through regular expression:

//This means two colons splitting three blocks of one to two digits.

So here's the complete code for getting the GMT:

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, traceTime);
loader.load(new URLRequest(""));

function traceTime(e:Event):void {
var str:String = URLLoader(;
var time:String = str.match(/\d{1,2}:\d{1,2}:\d{1,2}/)[0];

Oops, later on I found out that Google and Yahoo already provide a GMT inquiry service.
So my method was an overkill :p

No comments: