A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Birthday

  1. #1
    Junior Member
    Join Date
    Jun 2004
    Location
    Castellon, Spain
    Posts
    2

    Birthday

    Hi everyone! I'm a begnner in writing ActionScript and wonder if you could help me. The thing is I want a script that, having for example three dates:

    birthJohn= new Date(82, 8, 21)
    birthMary= new Date(78, 1, 25)
    birthPaul= new Date(54, 10, 9)

    and today's date:

    myDate=new Date()

    , tells you the nearest birthday and how old will that person be.

    Thank you!

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    A few notes about the following script.

    It uses an array to keep track of the names and dates. It then uses a loop to walk thru the array, checking each date in sequence.

    To compare dates the Date.getTime() function is used. This returns the date as a single number (milliseconds since 1970) which is useful when comparing the distance between dates. For example if I have two dates a and b and a.getTime() is less than b.getTime() then I know that a happened before b.

    code:


    // this is an array of three objects
    // each object has a name and a bday
    dbase = [{name:"John", bday:new Date(82,8,21)},
    {name:"Mary", bday:new Date(78,1,25)},
    {name:"Paul", bday:new Date(54,10,9)}];

    today = new Date();
    var year = today.getFullYear();

    var closestBDay = undefined;
    var closestIdx = 0;

    for (var i=0; i < dbase.length; ++i)
    {
    // for the person i
    // get their birthday for this year
    var nextBday = new Date(year, dbase[i].bday.getMonth(), dbase[i].bday.getDay());
    // check if it already happened
    if (nextBday.getTime() < today.getTime()) {
    // if so, get their birthday for next year
    // trace(dbase[i].name + ' already had a birthday this year');
    nextBday = new Date(year+1, dbase[i].bday.getMonth(), dbase[i].bday.getDay());
    }
    // check if it's the closest birthday so far, if so, keep track of it
    if (closestBDay == undefined || nextBday.getTime() - today.getTime() < closestBday.getTime() - today.getTime())
    {
    closestBday = nextBday;
    closestIdx = i;
    }
    }
    trace('The next birthday belongs to ' + dbase[closestIdx].name);
    trace('who will be ' + (closestBDay.getYear() - dbase[closestIdx].bday.getYear()) + ' years old');
    trace('on ' + closestBday);


    Last edited by jbum; 06-01-2004 at 05:39 AM.

  3. #3
    Junior Member
    Join Date
    Jun 2004
    Location
    Castellon, Spain
    Posts
    2
    You're great! It's even better than I thought!!
    Now: I'm creating some kind of "Flash enhaced" windows background, which would do, among other things, the "birthday thing" shown above.
    I do this by exporting a 1024x768 Flash to html and setting it as WXP background.

    Question: How would I do this with an external text file so that the user could edit his own birthdays without having access to the script?

    Thanks a lot.

  4. #4
    Junior Member
    Join Date
    Aug 2004
    Posts
    4
    First of all, it looks like a great code, however it's not working as it should be:

    Firstly, the variable names ar mistaken (closestBDay instead of closestBday).

    Secondly, even corrected, this code comes with different results when choosing new array elements:

    code:

    // this is an array of three objects
    // each object has a name and a bday
    dbase = [{name:"John", bday:new Date(88, 7, 21)},
    {name:"Mary", bday:new Date(88, 7, 29)},
    {name:"Paul", bday:new Date(88, 7, 12)}];

    today = new Date();

    var year = today.getFullYear();
    var closestBday = undefined;
    var closestIdx = 0;

    for (var i = 0; i<dbase.length; ++i) {
    // for the person i
    // get their birthday for this year
    var nextBday = new Date(year, dbase[i].bday.getMonth(), dbase[i].bday.getDay());
    // check if it already happened
    if (nextBday.getTime()<today.getTime()) {
    // if so, get their birthday for next year
    // trace(dbase[i].name + ' already had a birthday this year');
    nextBday = new Date(year+1, dbase[i].bday.getMonth(), dbase[i].bday.getDay());
    }
    // check if it's the closest birthday so far, if so, keep track of it
    if (closestBday == undefined || nextBday.getTime()-today.getTime()<closestBday.getTime()-today.getTime()) {
    closestBday = nextBday;
    closestIdx = i;
    }
    }
    trace('The next birthday belongs to '+dbase[closestIdx].name);
    trace('who will be '+(closestBday.getYear()-dbase[closestIdx].bday.getYear())+' years old');
    trace('on '+closestBday);



    For some reason, the output is John (August 21), although it should be Paul (August 12).

    I can't seem to find an error here, therefore any suggestions/thoughts are highly appreciated...

    I desperately need your help here...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center