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

Notifications

Announcements

No record found.

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

I have the same question (0)
  • 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,438 User Group Leader 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans