Skip to main content
February 4, 2025
Solved

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

  • February 4, 2025
  • 3 replies
  • 0 views

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. 

Best answer by csteward

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)
)

3 replies

csteward
cstewardAnswer
February 4, 2025

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)
)

varuntejg243705
February 4, 2025

Hi [mention:9821b635d3254a04b83d2743a02c782f:e9ed411860ed4f2ba0265705b8793d05] ,

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.

andrewh6762
February 4, 2025

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
)