<p>I've done this by populating a dropdown field using the <a href="https://github.com/lewisjenkins/craft-lj-dynamicfields" rel="nofollow">Dynamic Fields</a> plugin. Here's the code I used to generate the dropdown options:</p>

<pre><code>{% set maxYear = now|date_modify('+3 years')|date('Y') %}
{% for year in maxYear..1945 %}
    { "value": "{{ year }}", "label": "{{ year }}" }
    {% if not loop.last %},{% endif %}
{% endfor %}
</code></pre>

<p><em>Update:</em><br>
Re-reading your question I just noticed that you might not only need a year picker. Well you could add two more dropdown fields for months and days (1-31) or you just teach your client to input the date into the normal Date/Time field with the keyboard.</p>

CC BY-SA 3.0
2015-10-30T12:57:42.500Z
I was considering Dynamic Fields already (and putting 3 of them in a SuperTable to avoid using too much space). It would be nice to store the date in a single field though (and in proper date format). Thanks for the code!
Jure Srpcic
CC BY-SA 3.0
2015-10-30T13:01:52.097Z
You could use the Preparse plugin to combine it to a single field. That's actually a good idea, especially if you're using the dropdowns in a Super Table / Matrix. Makes it easy to output in the template or even use the field as a field parameter in a criteria model.
carlcs
CC BY-SA 3.0
2015-10-30T13:28:30.687Z
Good idea - being able to store age directly as a field. I'll add the final code as an answer for anyone dealing with a similar situation.
Jure Srpcic
CC BY-SA 3.0
2015-10-30T13:31:34.350Z
Don't store the age. Store the day in 2015-10-30 format and you can use Twig's `date` filter in your templates to output in any format you want, even the weekday.
carlcs
CC BY-SA 3.0
2015-10-30T13:33:58.260Z
Added as an option in comment.
Jure Srpcic
CC BY-SA 3.0
2015-10-30T11:58:47.453Z
2015-10-30T12:15:29.910Z
2015-10-30T12:15:29.910Z
carlcs
<p>Using carlcs's idea, I put three LJ Dynamic Dropdowns in a SuperTable, here's the code for each dropdown (day, month, year):</p>

<pre><code>{% for day in 1..31 %}
    { "value": "{{ day }}", "label": "{{ day }}" }
    {% if not loop.last %},{% endif %}
{% endfor %}

{% for month in 1..12 %}
    { "value": "{{ month }}", "label": "{{ month }}" }
    {% if not loop.last %},{% endif %}
{% endfor %}

{% set maxYear = now|date_modify('-15 years')|date('Y') %}
{% for year in maxYear..1925 %}
    { "value": "{{ year }}", "label": "{{ year }}" }
    {% if not loop.last %},{% endif %}
{% endfor %}
</code></pre>

<p>Then I added a Preparse field that joins these three dropdowns into a date string on each save (I have this under a User, so adapt to Entry/Asset/etc. if you need to).</p>

<pre><code>{% spaceless %}
{% if user.userDateOfBirth | length %}

    {% set dateString = user.userDateOfBirth[0].year ~ '-' ~ user.userDateOfBirth[0].month ~ '-' ~ user.userDateOfBirth[0].day %}
    {{ dateString }}

{% endif  %}
{% endspaceless %}
</code></pre>

<p>To calculate age in my template, I use this little snippet (date("2015-07-06") will return a DateTime object so we can use the diff function on it).</p>

<pre><code>{% set ageString = date(dateString).diff(now).format('%y') %}
</code></pre>

CC BY-SA 3.0
2015-10-30T13:31:51.207Z
2015-10-30T13:55:18.780Z
2015-10-30T13:55:18.780Z
Jure Srpcic
<p>I need a "date of birth" field. Out of the box, the date picker only allows moving left/right by month, which doesn't work for selecting years.</p>

<p>Is there a way to configure the date picker that Craft uses? Or is there a better way to make a date of birth chooser?</p>

<p>Thanks!</p>

<field><datetime>
Date picker field type - quickly select year?
# Date picker field type - quickly select year?

(Asked by: username_0 on 2015-10-30)

I need a "date of birth" field. Out of the box, the date picker only allows moving left/right by month, which doesn't work for selecting years.

Is there a way to configure the date picker that Craft uses? Or is there a better way to make a date of birth chooser?

Thanks!

**Tags:** field, datetime


---

## 2 answers

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

I've done this by populating a dropdown field using the Dynamic Fields plugin. Here's the code I used to generate the dropdown options:
{% set maxYear = now|date_modify('+3 years')|date('Y') %}
{% for year in maxYear..1945 %}
    { "value": "{{ year }}", "label": "{{ year }}" }
    {% if not loop.last %},{% endif %}
{% endfor %}

Update:
Re-reading your question I just noticed that you might not only need a year picker. Well you could add two more dropdown fields for months and days (1-31) or you just teach your client to input the date into the normal Date/Time field with the keyboard.

> **Comments:**
> **username_0**: I was considering Dynamic Fields already (and putting 3 of them in a SuperTable to avoid using too much space). It would be nice to store the date in a single field though (and in proper date format). Thanks for the code!  
> **username_1**: You could use the Preparse plugin to combine it to a single field. That's actually a good idea, especially if you're using the dropdowns in a Super Table / Matrix. Makes it easy to output in the template or even use the field as a field parameter in a criteria model.  
> **username_0**: Good idea - being able to store age directly as a field. I'll add the final code as an answer for anyone dealing with a similar situation.  
> **username_1**: Don't store the age. Store the day in 2015-10-30 format and you can use Twig's `date` filter in your templates to output in any format you want, even the weekday.  
> **username_0**: Added as an option in comment.  

---

### Answer by username_0 on 2015-10-30. Score: 3

Using username_1's idea, I put three LJ Dynamic Dropdowns in a SuperTable, here's the code for each dropdown (day, month, year):
{% for day in 1..31 %}
    { "value": "{{ day }}", "label": "{{ day }}" }
    {% if not loop.last %},{% endif %}
{% endfor %}

{% for month in 1..12 %}
    { "value": "{{ month }}", "label": "{{ month }}" }
    {% if not loop.last %},{% endif %}
{% endfor %}

{% set maxYear = now|date_modify('-15 years')|date('Y') %}
{% for year in maxYear..1925 %}
    { "value": "{{ year }}", "label": "{{ year }}" }
    {% if not loop.last %},{% endif %}
{% endfor %}

Then I added a Preparse field that joins these three dropdowns into a date string on each save (I have this under a User, so adapt to Entry/Asset/etc. if you need to).
{% spaceless %}
{% if user.userDateOfBirth | length %}

    {% set dateString = user.userDateOfBirth[0].year ~ '-' ~ user.userDateOfBirth[0].month ~ '-' ~ user.userDateOfBirth[0].day %}
    {{ dateString }}

{% endif  %}
{% endspaceless %}

To calculate age in my template, I use this little snippet (date("2015-07-06") will return a DateTime object so we can use the diff function on it).
{% set ageString = date(dateString).diff(now).format('%y') %}


---

