Increment dates by months in X++
PA-22040759-0
6,194
A question was asked in the forums, about how to increment dates by months like:
21/02/2015
21/03/2015
21/04/2015
etc.
nextMth and dateMthFwd was suggested, so I just wanted to list some options.
Consider these examples.
nextMth will be off by days after hitting uneven months:
dateMthFwd works ok:
And System.DateTime.AddMonths works ok:
21/02/2015
21/03/2015
21/04/2015
etc.
nextMth and dateMthFwd was suggested, so I just wanted to list some options.
Consider these examples.
nextMth will be off by days after hitting uneven months:
static void TestNextMth(Args _args)
{
date d = nextMth(31\12\2015);
print d;
d = nextMth(d);
print d;
d = nextMth(d);
print d;
d = nextMth(d);
print d;
pause;
}
- 31/1/2016
- 29/2/2016
- 29/3/2016
- 29/4/2016
dateMthFwd works ok:
static void TestDateMthFwd(Args _args)
{
date d = mkDate(31, 12, 2015);
print dateMthFwd(d, 1);
print dateMthFwd(d, 2);
print dateMthFwd(d, 3);
print dateMthFwd(d, 4);
pause;
}
- 31/1/2016
- 29/2/2016
- 31/3/2016
- 30/4/2016
And System.DateTime.AddMonths works ok:
static void TestAddMonths(Args _args)
{
System.DateTime dateTime = new System.DateTime(2015, 12, 31);
print dateTime.AddMonths(1);
print dateTime.AddMonths(2);
print dateTime.AddMonths(3);
print dateTime.AddMonths(4);
pause;
}
- 31/1/2016
- 29/2/2016
- 31/3/2016
- 30/4/2016
*This post is locked for comments