web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

how to get month + year between two dates

(0) ShareShare
ReportReport
Posted on by 385

Dear All,

     I want to get list of month + year  between  two dates and save in array,

and also how to to use array for date in x++

how to do this please help me 

*This post is locked for comments

  • Verified answer
    Kamesh021 Profile Picture
    165 on at

    Dear azam,

    The following is the code to get list of months between two dates, reply whether your requirements are satisfied or not.

    static void Job21(Args _args)

    {

       str dateMonth[];

       TransDate date1, date2;

       int month1, month2, counter, i, cnt, y;

       ;

       date1 = mkDate(1, 1, 2016);

       date2 = mkDate(31, 11, 2016);

       month1 = mthOfYr(date1);

       month2 = mthOfYr(date2);

       y = year(date1);

       if(month2 > month1)

           counter = month2 - month1;

       else

           counter = month1 - month2;

       cnt = counter;

       info(strFmt("%1 is year", y));

       for( i = 1; i <=counter; i++)

       {

           dateMonth[i] = mthName(cnt);

           info(strFmt("%1", dateMonth[i]));

           cnt--;

       }

    }

    Thanks and regards

    Kamesh K

  • Verified answer
    Sohaib Cheema Profile Picture
    49,668 Super User 2026 Season 2 on at

    Above given job is not taking care of year.. Put the two dates in two different years and you will see results. You may try with following job.

    static void YearMonthCombinations(Args _args)
    {
        //<variables for dialog and dates>
        Dialog      dialog;
        DialogField dfFromDate,dfToDate;
        FromDate    vFromDate;
        ToDate      vToDate;
        date        nextDate;
        //</variables for dialog and dates>
    
        //<variables for array>
        date DemoArray[];
        int  counterIndex;
        int  outputCounter;
        //</variables for array>
        ;
    
        dialog = new Dialog("Year-Month Combinations");
    
        dfFromDate = dialog.addField(extendedTypeStr(FromDate));
        dfToDate = dialog.addField(extendedTypeStr(ToDate));
    
        if(dialog.run())
        {
            vFromDate   =   dfFromDate.value();
            vToDate     =   dfToDate.value();
    
            counterIndex = 1;
            nextDate = vFromDate;
    
            
            info(strFmt('Date: %1 Year:%2 Month%2',nextDate,year(nextDate),mthName(mthOfYr(nextDate))));
            do
            {
                nextDate = dateMthFwd(nextDate,1);
                if(nextDate <= vToDate)
                {
                    //info(strFmt('%1',nextDate));
                    //add value in array
                    DemoArray[counterIndex] = nextDate;
                    counterIndex++;
                }
            }
            while(nextDate <= vToDate);
    
            for(outputCounter=1; outputCounter<=counterIndex-1; outputCounter++)
            {
               info(strFmt('Date: %1 Year:%2 Month:%2',DemoArray[outputCounter],year(DemoArray[outputCounter]),mthName(mthOfYr(DemoArray[outputCounter]))));
            }
    
        }
    
    }


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans