-
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.
-
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.)
-
Senior Member
Is that a polite way of saying RTFM ?
Last edited by jbum; 09-30-2004 at 01:28 PM.
-
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;
}
-
Senior Member
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));
-
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.
-
Senior Member
Yes, you are right - you should use floor() or int() there - I forgot that actionscript doesn't do integer division.
Good call!
-
Re Member
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.
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|