<p>The <code>year</code> and  <code>month</code> variables are not defined, so you'd first have to get these from the 'date' variable.</p>

<p>You could do this by using Twig's <code>split</code> function and splitting <code>date</code> by the space in between:</p>

<pre><code>{% set dateParts = date | split(' ') %}
{% set month = dateParts[1] %}
{% set year = dateParts[0] %}

&lt;a href="{{ year }}/{{ month }}"&gt;{{ date }}&lt;/a&gt;
</code></pre>

CC BY-SA 3.0
2015-01-09T18:05:04.503Z
2015-01-09T18:05:04.503Z
Paul
<p>I was going to suggest using twig's <code>date</code> filter to format the date as needed, but as Paul astutely pointed out in his comment, you don't really have a dateTime variable to work with. </p>

<p>So my answer is use Paul's answer. ;)</p>

CC BY-SA 3.0
2015-01-09T18:06:16.510Z
Douglas, am I wrong or is `date` a string, not a DateTime object?
Paul
CC BY-SA 3.0
2015-01-09T18:16:39.517Z
It is. I noticed it as soon as I posted and fixed it. Thanks
Douglas McDonald
CC BY-SA 3.0
2015-01-09T18:19:11.903Z
Douglas, this wouldn't work. Your `entry` variable now contains all entries, so you wouldn't be able to get a `postDate` from it ;)
Paul
CC BY-SA 3.0
2015-01-09T18:33:55.683Z
You're right Paul! Remind me never to answer questions before my first cup of coffee please.
Douglas McDonald
CC BY-SA 3.0
2015-01-09T18:34:34.560Z
Coffee? Just finishing my first glass of whisky ;)
Paul
CC BY-SA 3.0
2015-01-09T18:05:17.417Z
2015-01-09T18:30:05.977Z
2015-01-09T18:30:05.977Z
Douglas McDonald
<p>If I understand your example correctly, a simple hyphen would do the trick &mdash; "YYYY-MM" is a format the <code>date</code> filter recognizes.</p>

<pre><code>{% set entries = ... %}
{% set entriesByMonth = entries|group('postDate|date("Y-m")') %}
{% for month, entries in entriesByMonth %}
  &lt;li&gt;
      &lt;a href="{{ month|date("Y") }}/{{ month|date("m") }}"&gt;
        {{ entries | length }} entries in {{ month|date("F Y") }}
      &lt;/a&gt;
  &lt;/li&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-01-12T14:50:37.007Z
I actually like Micahel's answer the best as there is no extra variables needed.
Siebird
CC BY-SA 3.0
2015-01-10T00:56:42.673Z
2015-01-10T00:56:42.673Z
Michael Rog
<p>Having issues trying to access the year &amp; month variable when grouping entries by month-year. The code is outputting the desired results (ie. December 2014), however I would like to format the url by 'year/month'.</p>

<h3>Archives</h3>

<p>
    <p>Browse news &amp; articles for the last year.</p></p>

<pre><code>{% set allEntries = craft.entries.section('news').limit(null) %}
&lt;ul&gt;
{% for date, entries in allEntries | group('postDate|date("F Y")') %}
    &lt;li&gt;&lt;a href="{{ year }}/{{ month }}"&gt;{{ date }}&lt;/a&gt;&lt;/li&gt;
{% endfor %}
&lt;/ul&gt;
</code></pre>

<p> </p>

CC BY-SA 3.0
2015-01-09T18:16:02.077Z
Both answers give me the desired results. Now I'm wondering what is the better answer? Paul's splits the `date` variable and Douglas use the DateTime object
Siebird
CC BY-SA 3.0
2015-01-10T00:59:38.200Z
Douglass is on the right track. You can *get* a DateTime to [re]format if you your year-month variable is in a format the `date` filter recognizes. I added a code snippet that I hope illustrates this helpfully.
Michael Rog
<entries><group>
Need to access year/month variable when grouping entries by date
# Need to access year/month variable when grouping entries by date

(Asked by: username_0 on 2015-01-09)

Having issues trying to access the year & month variable when grouping entries by month-year. The code is outputting the desired results (ie. December 2014), however I would like to format the url by 'year/month'.
Archives

Browse news & articles for the last year.
{% set allEntries = craft.entries.section('news').limit(null) %}

{% for date, entries in allEntries | group('postDate|date("F Y")') %}
    {{ date }}
{% endfor %}

**Tags:** entries, group

> **Comments:**
> **username_0**: Both answers give me the desired results. Now I'm wondering what is the better answer? username_2's splits the `date` variable and Douglas use the DateTime object  
> **username_1**: Douglass is on the right track. You can *get* a DateTime to [re]format if you your year-month variable is in a format the `date` filter recognizes. I added a code snippet that I hope illustrates this helpfully.  

---

## 3 answers

### Answer by username_1 on 2015-01-10. Score: 10 | Accepted answer

If I understand your example correctly, a simple hyphen would do the trick — "YYYY-MM" is a format the date filter recognizes.
{% set entries = ... %}
{% set entriesByMonth = entries|group('postDate|date("Y-m")') %}
{% for month, entries in entriesByMonth %}
  
      
        {{ entries | length }} entries in {{ month|date("F Y") }}
      
  
{% endfor %}

> **Comments:**
> **username_0**: I actually like Micahel's answer the best as there is no extra variables needed.  

---

### Answer by username_2 on 2015-01-09. Score: 4

The year and  month variables are not defined, so you'd first have to get these from the 'date' variable.
You could do this by using Twig's split function and splitting date by the space in between:
{% set dateParts = date | split(' ') %}
{% set month = dateParts[1] %}
{% set year = dateParts[0] %}

{{ date }}


---

### Answer by username_3 on 2015-01-09. Score: 1

I was going to suggest using twig's `date` filter to format the date as needed, but as username_2 astutely pointed out in his comment, you don't really have a dateTime variable to work with.

So my answer is use username_2's answer. ;)

> **Comments:**
> **username_2**: Douglas, am I wrong or is `date` a string, not a DateTime object?  
> **username_3**: It is. I noticed it as soon as I posted and fixed it. Thanks  
> **username_2**: Douglas, this wouldn't work. Your `entry` variable now contains all entries, so you wouldn't be able to get a `postDate` from it ;)  
> **username_3**: You're right username_2! Remind me never to answer questions before my first cup of coffee please.  
> **username_2**: Coffee? Just finishing my first glass of whisky ;)  

---

