How could we get the months between the start date and end date

Hi All,

Currently I am working on a requirement where we need to compute the months based on start date and end date. Let me say for example start date 03/11/2016 and end date is 03/12/2017. The months between the dates should be " Dec-16", "Jan-17", 'Feb-17", How could we achieve this functionality?

  Discussion posts and replies are publicly visible

Parents
  • Hi Vinod,

    You can use below code to achieve the functionality you are after. If you want to use it to a project please add validation to it as I have not done any.

    load(
      local!monthNUmber:{1,2,3,4,5,6,7,8,9,10,11,12},
      local!monthName:{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"},
    with(
       local!years:year(ri!todate)-year(ri!fromDate),
       local!months:(local!years)*12 + month(ri!todate)-month(ri!fromdate)+1, /*Derive number of months in between two dates */
       local!enumArray: rdrop(1 + enumerate(local!months),1),  /*derive the array to iretare through*/
       local!thisYear: year(ri!fromdate),
       local!thisMonth: month(ri!fromdate),
       local!thisMonthYear: fn!displayvalue(local!thisMonth,{local!monthNUmber},{local!monthName},"") & "-" &right(local!thisYear,2), /*Get first month year */
    
       append({local!thisMonthYear},
           apply(rule!TEST_appendYearMonth,local!enumArray,local!monthNUmber,local!monthName,ri!fromdate)      
        ) /*generate list of month year*/
     )
    )

    rule called in apply has below structure and code in it. Just fiddle around if need to change the order how the Month-year should appear
    rule!TEST_appendYearMonth(int arrayInstance,int array monthNumList, text array monthNameList, date fromDate)

    fn!displayvalue(month(usereomonth(ri!fromDate,ri!arrayInstance)),ri!monthNumList,ri!monthNameList,"") 
           &"-"&
    right(year(usereomonth(ri!fromDate,ri!arrayInstance)),2)

     

    Regards

    Suresh

Reply
  • Hi Vinod,

    You can use below code to achieve the functionality you are after. If you want to use it to a project please add validation to it as I have not done any.

    load(
      local!monthNUmber:{1,2,3,4,5,6,7,8,9,10,11,12},
      local!monthName:{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"},
    with(
       local!years:year(ri!todate)-year(ri!fromDate),
       local!months:(local!years)*12 + month(ri!todate)-month(ri!fromdate)+1, /*Derive number of months in between two dates */
       local!enumArray: rdrop(1 + enumerate(local!months),1),  /*derive the array to iretare through*/
       local!thisYear: year(ri!fromdate),
       local!thisMonth: month(ri!fromdate),
       local!thisMonthYear: fn!displayvalue(local!thisMonth,{local!monthNUmber},{local!monthName},"") & "-" &right(local!thisYear,2), /*Get first month year */
    
       append({local!thisMonthYear},
           apply(rule!TEST_appendYearMonth,local!enumArray,local!monthNUmber,local!monthName,ri!fromdate)      
        ) /*generate list of month year*/
     )
    )

    rule called in apply has below structure and code in it. Just fiddle around if need to change the order how the Month-year should appear
    rule!TEST_appendYearMonth(int arrayInstance,int array monthNumList, text array monthNameList, date fromDate)

    fn!displayvalue(month(usereomonth(ri!fromDate,ri!arrayInstance)),ri!monthNumList,ri!monthNameList,"") 
           &"-"&
    right(year(usereomonth(ri!fromDate,ri!arrayInstance)),2)

     

    Regards

    Suresh

Children