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
  • 0
    Certified Lead Developer
    Hi Vinod,

    Please use the below code snippet in an expression rule which fulfills the above mentioned requirement.

    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(_, cons!TEST_MONTH_NUMBERS, cons!TEST_MONTHS, null), {enumerate(local!toMonth) + local!fromMonth})}, {null})}),
    if(local!fromYear < local!toYear,
    append(
    fn!apply(append(_, "-"&right(local!fromYear, 2)), {difference({fn!apply(fn!displayvalue(_, cons!TEST_MONTH_NUMBERS, cons!TEST_MONTHS, null), {enumerate(12) + local!fromMonth})}, {null})}),
    fn!apply(append(_, "-"&right(local!toYear, 2)), {difference({fn!apply(fn!displayvalue(_, cons!TEST_MONTH_NUMBERS, cons!TEST_MONTHS, null), {enumerate(local!toMonth) + 1})}, {null})})
    ),
    null
    )
    )
    )
    )
    )

    cons!TEST_MONTH_NUMBERS: Contains {1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12} array.
    cons!TEST_MONTHS: Contains {Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec} array

    Please let me know if you face any trouble in executing the code snippet

    Thanks,
    Ramesh
  • 0
    A Score Level 1
    in reply to Ram
    Thank you so much - Ramesh,
    It is working fine if the year is different.
    But if year is same( example: start date: 04/04/2017, end Date: 10/04/2017) displaying from Apr-17 to Dec-17.
    displaying 3+ months .
  • 0
    Certified Lead Developer
    in reply to vinod alla
    Hi Vinod,

    Try replacing the below code i.e. code at line 11.

    fn!apply(append(_, "-"&right(local!fromYear, 2)), {difference({fn!apply(fn!displayvalue(_, cons!TEST_MONTH_NUMBERS, cons!TEST_MONTHS, null), {enumerate(local!toMonth) + local!fromMonth})}, {null})})

    with

    fn!apply(append(_, "-"&right(local!fromYear, 2)), {difference({fn!apply(fn!displayvalue(_, cons!TEST_MONTH_NUMBERS, cons!TEST_MONTHS, null), {enumerate(local!toMonth + 1 - local!fromMonth) + local!fromMonth})}, {null})}),

    Thanks,
    Ramesh
  • 0
    A Score Level 1
    in reply to Ram
    Perfect - Ramesh, Now working as expected.

    Thank you so much
Reply Children