Snippet – Converting yyyy-mm-ddThh:mm:ss.mssZ to Date()

Hi,

So, in dealing with the Google Data API most dates use the format above. Here is a regular expression to decompose it:

1
2
3
4
5
6
7
8
9
10
11
12
13
var date:String = "2010-12-12T09:54:35.002Z";
 
var exp:RegExp = /(\d\d\d\d)([- \/.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])T([0-2][0-9]):([0-5][0-9]):([0-5][0-9]).([0-9]{3})Z/i;
 
var res:Object = exp.exec(date);
 
// year res[1]
// month res[3]
// day res[4]
// hour res[5]
// minute res[6]
// seconds res[7]
// milliseconds res[8]

Now remember that the constructor for the Date object is quite a pain. It considers Januaray to be month 0, and the first day of the month to be the zero’th day of the month.. With this in mind, you can construct the Date object.

1
var time:Date = new Date(res[1], res[3] - 1, res[4] - 1, res[5], res[6], res[7], res[8]);

Now, switch to the time-zone of the user..

1
time.minutes -= time.getTimezoneOffset();

That’s it folks!

3 comments on “Snippet – Converting yyyy-mm-ddThh:mm:ss.mssZ to Date()

  1. bereal on said:

    Thanks, looking for some time for this

  2. Hey Mart!
    How are you doing?
    It’s Mathieu C. from New World!

    Nice stuff you did for the GData!

    By the way there a small error in your date parsing it should be:
    var time:Date = new Date(res[1], res[3] – 1, res[4] , res[5], res[6], res[7], res[8]);
    day are from 1-31

    Do you any GData API that can post to youtube (comments, ratings)?

    See ya!

  3. mlegris on said:

    Salut Mathieu,

    if you download the youtube data api from source (subversion), you’ll notice there is code to post also. Not all the functionality is implemented however. You can even upload videos to youtube using the API, but because the response (304 if I remember well) doesn’t work on Safari for Mac I didn’t post anything about it. It works like a charm on all other browsers that I tested however.

    Look for YoutubeData in there, it should help. ;)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

36,560 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">