A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: add months to a yyyymm dateformat

  1. #1
    Senior Member
    Join Date
    Jul 2002
    Posts
    176

    add months to a yyyymm dateformat

    Hi,
    I am trying to create a function that calculates new date (yyyymm) based on the number of months inserted.
    e.g.
    function(200409,13)//results in 200510
    function (200409,1)//results in 200410

    can anyone help please?

    kind regards
    Patrick
    Last edited by cuboctahedron; 09-30-2004 at 01:43 PM.

  2. #2
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    Perhaps you should read up on the Date object in Flash, you know, their documentation is really good!

    code:

    var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var d = new Date(2004, 10);
    trace("Start date is "+months[d.getMonth()]+" in the year "+d.getFullYear());
    d.setMonth(d.getMonth()+11);
    trace("End date is "+months[d.getMonth()]+" in the year "+d.getFullYear());



    (PS: getMonth returns a zero indexed month number so 0 is January, 1 is February and so on.)

  3. #3
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Is that a polite way of saying RTFM ?
    Last edited by jbum; 09-30-2004 at 01:28 PM.

  4. #4
    Senior Member
    Join Date
    Jul 2002
    Posts
    176
    Hi have to following (working) function; perhaps not the most elegant, but workable though

    function addmonths(startdatum, maanden)
    {
    jaardeel=startdatum.substring(0,4);
    maanddeel=startdatum.substring(4,6);
    maandtotaal=Number(maanddeel)+Number(maanden);
    jaarverschil=Math.ceil(Number(maandtotaal)/12)-1;
    jaardeel=Number(jaardeel)+Number(jaarverschil);
    maanddeel=Number(maandtotaal)-(12*Number(jaarverschil));
    maanddeel=String(maanddeel);
    if (maanddeel.length==1){maanddeel="0"+maanddeel;}
    jaardeel=String(jaardeel);
    output=String(jaardeel)+String(maanddeel);

    return output;
    }

  5. #5
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    My version, using Coffey's example. I'm treating the YYYYMM string as a number, which simplifies things a bit.

    code:

    computeDateStr = function(dNum, monthOffset)
    {
    dNum = Number(dNum); // insure it's a number so I can do some math on it
    yr = dNum / 100;
    mn = dNum % 100;
    dt = new Date(yr,mn-1);
    dt.setMonth(dt.getMonth()+monthOffset);
    return dt.getFullYear()*100 + (dt.getMonth()+1);
    }

    trace(computeDateStr(200411,5));


  6. #6
    Senior Member
    Join Date
    Jul 2002
    Posts
    176
    Thanks!
    jbum's version certainly rules over mine.

    PS does it matter if I would use yr = Math.floor(dNum / 100); in your statement? It just seems more consistent leaving out decimals, incase they should occur.
    Last edited by cuboctahedron; 09-30-2004 at 01:57 PM.

  7. #7
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Yes, you are right - you should use floor() or int() there - I forgot that actionscript doesn't do integer division.

    Good call!

  8. #8
    Re Member websam's Avatar
    Join Date
    Jul 2000
    Location
    Australia (VIC)
    Posts
    660
    Sorry J, just a bit minor modification:

    Code:
    computeDateStr = function(dNum, monthOffset)
    {
            dNum = Number(dNum); // insure it's a number so I can do some math on it
            yr = Math.floor(dNum / 100);
            mn = dNum % 100;
            dt = new Date(yr,mn+monthOffset);
            return dt.getFullYear()*100 + dt.getMonth();
    }
    Last edited by websam; 10-01-2004 at 02:48 AM.

  9. #9
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    I had to look at that for a while to see what you had done differently. All very elegant.

    However, seeing as we are apparently trying to get this pedantically correct (and as JBum + websam have done all the hard work ) the return should probably be done as a string and yr, mn and dt should be declared using "var" so as to encapsulate them within the function and prevent any possible conflict with other values.

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