Update a given date on some conditions

Hi,

I have a requirement to update a date based on a condition that if the given date is of last 2 dates of that particular month, then I want my rule to return first date of the next month and it should be applicable for all the dates whether the month has 30 or 31 days or if it is February or leap year.

For eg: 27/02/2023 should be converted to 01/03/2023.

I have written following code for this, but my senior asks me to optimize the code and reduce the use of if conditions. Please tell me what else I can do

edit:

a!localVariables(
  local!currentMonth: month(ri!requestDate),
  local!currentDay: day(ri!requestDate),
  local!currentYear: year(ri!requestDate),
  local!isLeapYear: isleapyear(local!currentYear),
  todate(
    if(
      and(
        tointeger(local!currentMonth) = 2,
        or(local!isLeapYear),
        contains(
          tointeger({ 28, 29 }),
          tointeger(local!currentDay)
        )
      ),
      concat(
        tointeger(local!currentMonth) + 1,
        "/",
        1,
        "/",
        local!currentYear
      ),
      if(
        and(
          tointeger(local!currentMonth) = 2,
          not(or(local!isLeapYear)),
          contains(
            tointeger({ 27, 28 }),
            tointeger(local!currentDay)
          )
        ),
        concat(
          tointeger(local!currentMonth) + 1,
          "/",
          1,
          "/",
          local!currentYear
        ),
        if(
          and(
            tointeger(local!currentMonth) <> 2,
            contains(
              tointeger({ 30, 31 }),
              tointeger(local!currentDay)
            )
          ),
          concat(
            if(
              tointeger(local!currentMonth) = 12,
              1,
              local!currentMonth + 1
            ),
            "/",
            1,
            "/",
            if(
              tointeger(local!currentMonth) = 12,
              local!currentYear + 1,
              local!currentYear
            )
          ),
          ri!requestDate
        )
      )
    )
  )
)

Thanks you for help

  Discussion posts and replies are publicly visible

Parents
  • For the longest time I didn't know about the function "eomonth()" and it probably caused me a lot of heartburn - i figure it might help you out here.

    I'm a little unclear what's being requested when ri!requestDate isn't one of the last 2 days of the given month, but this seems to work for what I understand of your requirement:

    a!localVariables(
      
      local!eomonthDate: eomonth(ri!requestDate, 0),
      local!nextMonthStartDate: local!eomonthDate + 1,
      
      local!isApplicableDate: or(
        local!eomonthDate = {ri!requestDate, ri!requestDate + 1}
      ),
      
      if(
        local!isApplicableDate,
        local!nextMonthStartDate,
        ri!requestDate
      )
    )

Reply
  • For the longest time I didn't know about the function "eomonth()" and it probably caused me a lot of heartburn - i figure it might help you out here.

    I'm a little unclear what's being requested when ri!requestDate isn't one of the last 2 days of the given month, but this seems to work for what I understand of your requirement:

    a!localVariables(
      
      local!eomonthDate: eomonth(ri!requestDate, 0),
      local!nextMonthStartDate: local!eomonthDate + 1,
      
      local!isApplicableDate: or(
        local!eomonthDate = {ri!requestDate, ri!requestDate + 1}
      ),
      
      if(
        local!isApplicableDate,
        local!nextMonthStartDate,
        ri!requestDate
      )
    )

Children
No Data