How do I pass dynamic year and month to the .after() and .before() functions?
In Twig, you concatenate strings with the ~ operator. So, if year and month are variables in your template, you can create a string in the required format like this

{% set yearMonth = year ~ '-' ~ month %}