Want to get 2nd Wednesday for the month and year I have given

Hi everyone,

I have a use case where a due date will be the 2nd Wednesday of the next month of given month and year. 

  Discussion posts and replies are publicly visible

  • Here is a quick method by looping to find the second Wednesday of next month:

    a!localVariables(
      local!targetMonth: if(ri!month=12,1,ri!month+1),
      local!targetYear: if(ri!month=12,ri!year+1,ri!year),
      local!targetDay: index(
        reject(
          a!isNullOrEmpty,
          a!forEach(
            items: 1+enumerate(daysinmonth(local!targetMonth,local!targetYear)),
            expression: if(weekday(date(local!targetYear,local!targetMonth,fv!item),1)=4,fv!item,null)
          )
        ),
        2,
        null
      ),
      
      date(local!targetYear,local!targetMonth,local!targetDay)
    )

  • +1
    Certified Associate Developer

    Hi  ,

    Please check the below code : 

    a!localVariables(
      local!dates: a!forEach(
        items: enumerate(daysinmonth(ri!month + 1, ri!year)) + 1,
        expression: { date(ri!year, ri!month + 1, fv!item) }
      ),
      local!weeks: weekday(local!dates),
      index(
        local!dates,
        index(wherecontains(4, local!weeks), 3)
      )
    )

    I hope this code is performant and reduce complexity.

  • Here's a method which goes to the first day of the month after now() and calculates the offset to the first Wednesday then just adds 7 days to get to the second Wednesday. The local!now variable was just to easily test. Code can easily be adjusted if the input isn't just a date and time (ie. If you needed to specify a month and year).

    a!localVariables(
      local!now: a!addDateTime(startDateTime: now(), months: 0),
      local!firstDayOfNextMonth: eomonth(local!now,0) + 1,
      local!dayValueOfFirstDayInMonth: weekday(local!firstDayOfNextMonth),
      local!firstWednesdayOfMonthOffset: mod(7 - local!dayValueOfFirstDayOfMonth + 4, 7),
      local!firstWednesdayOfMonth: local!firstDayOfNextMonth + local!firstWednesdayOfMonthOffset,
      local!secondWednesdayOfMonth: local!firstWednesdayOfMonth + 7,
      local!secondWednesdayOfMonth
    )