Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

how to get month + year between two dates

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
    Sohaib Cheema Profile Picture
    Sohaib Cheema 46,610 User Group Leader on at
    RE: how to get month + year between two dates

    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]))));
            }
    
        }
    
    }


  • Verified answer
    Kamesh021 Profile Picture
    Kamesh021 165 on at
    RE: how to get month + year between two dates

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans