Answers.Body: <p>In Twig, you <a href="http://twig.sensiolabs.org/doc/templates.html#other-operators" rel="nofollow">concatenate strings with the ~ operator</a>. So, if year and month are variables in your template, you can create a string in the required format like this</p>
  
  <pre><code>{% set yearMonth = year ~ '-' ~ month %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-10T14:13:04.623Z
Answers.Text: Perfect, hadn't thought to do an interim variable; thanks.
Answers.UserDisplayName: Matt Wilcox
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-10T14:25:09.683Z
Answers.Text: You can do this directly inside after too: craft.entries.section('news').limit(null).after(year~'-'~month).before(year~'-'~(month+1))
Answers.UserDisplayName: André Elvan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-10T13:05:32.343Z
Answers.LastActivityDate: 2014-12-10T15:48:21.080Z
Answers.LastEditDate: 2014-12-10T15:48:21.080Z
Answers.UserDisplayName: André Elvan
Answers.Body: <p>I've worked around this as follows:</p>
  
  <pre><code>{% if month is not defined %}
      {% set before = year+1 %}
      {% set after  = year %}
  {% else %}
      {% if month == 12 %}
          {% set yearBefore = year + 1 %}
          {% set monthBefore = '01' %}
  
          {% set before = yearBefore~'-'~monthBefore %}
          {% set after = year~'-'~month %}
      {% else %}
          {% set before = year ~ '-' ~ (month+1) %}
          {% set after  = year ~ '-' ~ month %}
      {% endif %}
  {% endif %}
  
  {% set entriesInPeriod = craft.entries.section('news').limit(null).after(after).before(before) %}
  </code></pre>
  
  <p>Which seems to be working well. Thanks all.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-10T16:30:35.037Z
Answers.Text: I think I was adding an answer at the same time as you. Glad you found a solution. Keep in mind, you may need to zero-pad the month if it is a single digit using `"%02d"|format(month + 1)`. Although I'm not sure — it may accept single digit month values.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-10T16:18:03.800Z
Answers.LastActivityDate: 2014-12-10T16:18:03.800Z
Answers.UserDisplayName: Matt Wilcox
Answers.Body: <p>This should solve the month = 13 problem.</p>
  
  <pre><code>{% set startDate = year ~ '-' ~ month %}
  {% if month == 12 %}
      {% set endDate = (year + 1) ~ '-01' %}
  {% else %}
      {% set endDate = year ~ '-' ~ "%02d"|format(month + 1) %}
  {% endif %}
  
  {% set entries = craft.entries.section('news').limit(null).after(startDate).before(endDate) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-10T16:12:54.923Z
Answers.LastActivityDate: 2014-12-10T16:24:56.780Z
Answers.LastEditDate: 2014-12-10T16:24:56.780Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>I've a URL /news/archive/2014/12</p>
  
  <p>A route parses that as going to an archive template and captures a year and month variable. I then want to select all the entries from that month, and I can't work out how. Here's what works for just the year:</p>
  
  <pre><code>{% set entriesInPeriod = craft.entries.section('news').limit(null).after(year).before(year+1) %}
  </code></pre>
  
  <p>And the docs say the after and before functions need things specified in the format:</p>
  
  <pre><code>YYYY
  YYYY-MM
  </code></pre>
  
  <p>I can't get that passed in because I don't know how Craft is interpolating the contents of the parenthasise; if I do</p>
  
  <pre><code>{% set entriesInPeriod = craft.entries.section('news').limit(null).after(year-month).before(year-month+1) %}
  </code></pre>
  
  <p>I'm not getting any results.</p>
  
  <p>UPDATE: Answer below (use ~ to concat strings) doesn't work correctly because in reality months 'loop' after 12. What gets put in the query when starting in December evaluates to 'month after 12 and before 13', which is garbage.</p>
  
  <p>Is there a way to get something like:</p>
  
  <pre><code>{% set entriesInPeriod = craft.entries.section('news').limit(null).postDate(year~'-'~month) %}
  </code></pre>
  
  <p>This doesn't work, but I can find no documentation on how postDate should be formatted in this use case.</p>
  
Tags: <entries>
Title: How do I pass dynamic year and month to the .after() and .before() functions?
ThreadText: # How do I pass dynamic year and month to the .after() and .before() functions?
  
  (Asked by: username_0 on 2014-12-10)
  
  I've a URL /news/archive/2014/12
  
  A route parses that as going to an archive template and captures a year and month variable. I then want to select all the entries from that month, and I can't work out how. Here's what works for just the year:
  
  ```
  {% set entriesInPeriod = craft.entries.section('news').limit(null).after(year).before(year+1) %}
  
  ```
  
  And the docs say the after and before functions need things specified in the format:
  
  ```
  YYYY
  YYYY-MM
  
  ```
  
  I can't get that passed in because I don't know how Craft is interpolating the contents of the parenthasise; if I do
  
  ```
  {% set entriesInPeriod = craft.entries.section('news').limit(null).after(year-month).before(year-month+1) %}
  
  ```
  
  I'm not getting any results.
  
  UPDATE: Answer below (use ~ to concat strings) doesn't work correctly because in reality months 'loop' after 12. What gets put in the query when starting in December evaluates to 'month after 12 and before 13', which is garbage.
  
  Is there a way to get something like:
  
  ```
  {% set entriesInPeriod = craft.entries.section('news').limit(null).postDate(year~'-'~month) %}
  
  ```
  
  This doesn't work, but I can find no documentation on how postDate should be formatted in this use case.
  
  **Tags:** entries
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2014-12-10. Score: 4
  
  In Twig, you [concatenate strings with the ~ operator](http://twig.sensiolabs.org/doc/templates.html#other-operators). 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 %}
  
  ```
  
  > **Comments:**
  > **username_0**: Perfect, hadn't thought to do an interim variable; thanks.  
  > **username_1**: You can do this directly inside after too: craft.entries.section('news').limit(null).after(year~'-'~month).before(year~'-'~(month+1))  
  
  ---
  
  ### Answer by username_0 on 2014-12-10. Score: 2
  
  I've worked around this as follows:
  
  ```
  {% if month is not defined %}
      {% set before = year+1 %}
      {% set after  = year %}
  {% else %}
      {% if month == 12 %}
          {% set yearBefore = year + 1 %}
          {% set monthBefore = '01' %}
  
          {% set before = yearBefore~'-'~monthBefore %}
          {% set after = year~'-'~month %}
      {% else %}
          {% set before = year ~ '-' ~ (month+1) %}
          {% set after  = year ~ '-' ~ month %}
      {% endif %}
  {% endif %}
  
  {% set entriesInPeriod = craft.entries.section('news').limit(null).after(after).before(before) %}
  
  ```
  
  Which seems to be working well. Thanks all.
  
  > **Comments:**
  > **username_2**: I think I was adding an answer at the same time as you. Glad you found a solution. Keep in mind, you may need to zero-pad the month if it is a single digit using `"%02d"|format(month + 1)`. Although I'm not sure — it may accept single digit month values.  
  
  ---
  
  ### Answer by username_2 on 2014-12-10. Score: 1
  
  This should solve the month = 13 problem.
  
  ```
  {% set startDate = year ~ '-' ~ month %}
  {% if month == 12 %}
      {% set endDate = (year + 1) ~ '-01' %}
  {% else %}
      {% set endDate = year ~ '-' ~ "%02d"|format(month + 1) %}
  {% endif %}
  
  {% set entries = craft.entries.section('news').limit(null).after(startDate).before(endDate) %}
  
  ```
  
  
  ---
  
  