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 .
Reply Children
  • 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
  • 0
    A Score Level 1
    in reply to Ram
    Ramesh,

    If the difference between ( local!toYear, local!fromYear )>1 only the boundary year months are displaying.

    For example start date Sep - 2015 and To date Feb -2017 , Displaying {Sep-15, Oct -15, Nov-15, Dec-15, Jan-17, Feb-17}
  • 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.