Dec 4, 2018

Get month of year range in X++

TransDate       _StartDate = str2Date("2018-01-31",321);
TransDate       _StartDateLoop = _StartDate;
TransDate       _EndDate = str2Date("2020-04-15",321);

while(str2Date(strFmt("%1-%2-%3",year(_StartDateLoop),mthOfYr(_StartDateLoop),"01"),321)
   <= str2Date(strFmt("%1-%2-%3",year(_EndDate),mthOfYr(_EndDate),"01"),321))
{
info(strFmt("%1",_StartDateLoop));

if (mthOfYr(_StartDateLoop) == 12)
{
_StartDateLoop = str2Date(strFmt("%1-%2-%3",year(_StartDateLoop)+1,"01","01"),321);
}
else
{
_StartDateLoop = str2Date(strFmt("%1-%2-%3",year(_StartDateLoop),mthOfYr(_StartDateLoop)+1,"01"),321);

if (year(_StartDateLoop) == year(_EndDate) && mthOfYr(_StartDateLoop) == mthOfYr(_EndDate))
{
_StartDateLoop = _EndDate;
}
}
}

Result
Info Message (03:24:28 pm) 2018-01-31
Info Message (03:24:28 pm) 2018-02-01
Info Message (03:24:28 pm) 2018-03-01
Info Message (03:24:28 pm) 2018-04-01
Info Message (03:24:28 pm) 2018-05-01
Info Message (03:24:28 pm) 2018-06-01
Info Message (03:24:28 pm) 2018-07-01
Info Message (03:24:28 pm) 2018-08-01
Info Message (03:24:28 pm) 2018-09-01
Info Message (03:24:28 pm) 2018-10-01
Info Message (03:24:28 pm) 2018-11-01
Info Message (03:24:28 pm) 2018-12-01
Info Message (03:24:28 pm) 2019-01-01
Info Message (03:24:28 pm) 2019-02-01
Info Message (03:24:28 pm) 2019-03-01
Info Message (03:24:28 pm) 2019-04-01
Info Message (03:24:28 pm) 2019-05-01
Info Message (03:24:28 pm) 2019-06-01
Info Message (03:24:28 pm) 2019-07-01
Info Message (03:24:28 pm) 2019-08-01
Info Message (03:24:28 pm) 2019-09-01
Info Message (03:24:28 pm) 2019-10-01
Info Message (03:24:28 pm) 2019-11-01
Info Message (03:24:28 pm) 2019-12-01
Info Message (03:24:28 pm) 2020-01-01
Info Message (03:24:28 pm) 2020-02-01
Info Message (03:24:28 pm) 2020-03-01
Info Message (03:24:28 pm) 2020-04-15

No comments:

Post a Comment