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

  • 0
    Certified Lead Developer
    in reply to vinod alla
    Hi Vinod,

    As per your requirement I have modified the code and pasted below.

    with(
    local!fromMonth: month(
    ri!fromDatetime
    ),
    local!toMonth: month(
    ri!toDatetime
    ),
    local!fromYear: year(
    ri!fromDatetime
    ),
    local!toYear: year(
    ri!toDatetime
    ),
    if(
    and(
    local!fromYear = local!toYear,
    local!fromMonth > local!toMonth
    ),
    "From date should be less than to date",
    if(
    local!fromYear > local!toYear,
    "From date should be less than to date",
    if(
    local!fromYear = local!toYear,
    fn!apply(
    append(
    _,
    "-" & right(
    local!fromYear,
    2
    )
    ),
    {
    difference(
    {
    fn!apply(
    fn!displayvalue(
    _,
    {
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
    11,
    12
    },
    {
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec"
    },
    null
    ),
    {
    enumerate(
    local!toMonth + 1 - local!fromMonth
    ) + local!fromMonth
    }
    )
    },
    {
    null
    }
    )
    }
    ),
    if(
    local!toYear - local!fromYear = 1,
    append(
    fn!apply(
    append(
    _,
    "-" & right(
    local!fromYear,
    2
    )
    ),
    {
    difference(
    {
    fn!apply(
    fn!displayvalue(
    _,
    {
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
    11,
    12
    },
    {
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec"
    },
    null
    ),
    {
    enumerate(
    12
    ) + local!fromMonth
    }
    )
    },
    {
    null
    }
    )
    }
    ),
    fn!apply(
    append(
    _,
    "-" & right(
    local!toYear,
    2
    )
    ),
    {
    difference(
    {
    fn!apply(
    fn!displayvalue(
    _,
    {
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
    11,
    12
    },
    {
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec"
    },
    null
    ),
    {
    enumerate(
    local!toMonth
    ) + 1
    }
    )
    },
    {
    null
    }
    )
    }
    )
    ),
    if(
    local!toYear - local!fromYear > 1,
    append(
    fn!apply(
    append(
    _,
    "-" & right(
    local!fromYear,
    2
    )
    ),
    {
    difference(
    {
    fn!apply(
    fn!displayvalue(
    _,
    {
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
    11,
    12
    },
    {
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec"
    },
    null
    ),
    {
    enumerate(
    12
    ) + local!fromMonth
    }
    )
    },
    {
    null
    }
    )
    }
    ),
    fn!apply(
    rule!getAllMonthsInAYear(
    _
    ),
    enumerate(
    (
    local!toYear - local!fromYear
    ) - 1
    ) + local!fromYear + 1
    ),
    fn!apply(
    append(
    _,
    "-" & right(
    local!toYear,
    2
    )
    ),
    {
    difference(
    {
    fn!apply(
    fn!displayvalue(
    _,
    {
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
    11,
    12
    },
    {
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec"
    },
    null
    ),
    {
    enumerate(
    local!toMonth
    ) + 1
    }
    )
    },
    {
    null
    }
    )
    }
    )
    ),
    null
    )
    )
    )
    )
    )
    )

    I haven't applied any date range to the code which might cause performance issue if the difference between two years is huge.

    Note:

    code used in rule!getAllMonthsInAYear() is
    {
    "Jan"&"-"&right(ri!year, 2),
    "Feb"&"-"&right(ri!year, 2),
    "Mar"&"-"&right(ri!year, 2),
    "Apr"&"-"&right(ri!year, 2),
    "May"&"-"&right(ri!year, 2),
    "Jun"&"-"&right(ri!year, 2),
    "Jul"&"-"&right(ri!year, 2),
    "Aug"&"-"&right(ri!year, 2),
    "Sep"&"-"&right(ri!year, 2),
    "Oct"&"-"&right(ri!year, 2),
    "Nov"&"-"&right(ri!year, 2),
    "Dec"&"-"&right(ri!year, 2)
    }

    Let me know if you face any problems in executing the code.
  • 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