<p>You can add a new filter to twig that returns an empty string if the date is null or '0000-00-00', or if not reverts to Twig's built in date filter.
Here I've called it 'dateInclEmpty' <em>(trying to think of a better name)</em>, you can otherwise use it in the same way as the existing date filter.</p>

<pre><code>$filter = function(Twig_Environment $env){
    return new Twig_SimpleFilter('dateInclEmpty', function ($date,$format,$timezone) use($env){
        if (is_null($date) || $date == '0000-00-00'){
            return "";
        }
        else {
            return twig_date_format_filter($env,$date,$format,$timezone);
        }
    });
};
$twig-&gt;addFilter($filter($twig));
</code></pre>

<p><em>(Thanks for the advice Lindsey, let's see if this is any better :-P)</em></p>

CC BY-SA 3.0
2016-01-20T15:50:00.797Z
-1 Hacking the core is never a good idea :(
Lindsey D
CC BY-SA 3.0
2016-01-22T10:49:27.597Z
I've made it as a new filter instead, hopefully less hacky
leontrolski
CC BY-SA 3.0
2016-01-22T16:15:32.283Z
Much better! :)
Lindsey D
CC BY-SA 3.0
2016-01-20T10:50:08.647Z
2016-01-22T10:48:15.427Z
2016-01-22T10:48:15.427Z
leontrolski
<p>For DateTime:</p>

<pre><code>{{ entry.myDate =="0000-00-00 00:00:00" ? "" : entry.myDate|date('Y-m-d') }}
</code></pre>

<p>For Date :</p>

<pre><code>{{ entry.myDate =="0000-00-00" ? "" : entry.myDate|date('Y-m-d') }}
</code></pre>

CC BY-SA 4.0
2018-07-30T20:29:01.357Z
2018-07-30T21:48:50.143Z
2018-07-30T21:48:50.143Z
Hossein Bagherzadegan
<p>That happens because the <a href="http://twig.sensiolabs.org/doc/filters/date.html" rel="noreferrer"><code>date</code></a> filter returns the current date for null values / empty strings.</p>

<blockquote>
  <p>If the value passed to the date filter is null, it will return the current date by default.</p>
</blockquote>

<p>To work around this, you would add a conditional.</p>

<pre><code>{% if entry.myDate %}
    Date: {{ entry.myDate|date('Y-m-d') }}
{% endif %}
</code></pre>

<p>Another way to do this is to use a <a href="http://twig.sensiolabs.org/doc/templates.html#other-operators" rel="noreferrer">ternary operator</a>.</p>

<pre><code>{{ entry.myDate ? entry.myDate|date('Y-m-d') }}
</code></pre>

CC BY-SA 4.0
2020-03-19T12:02:06.133Z
for completeness `{{ entry.myDate ? entry.myDate|date('Y-m-d') : 'XYZ' }}`
caramba
CC BY-SA 3.0
2015-04-17T11:37:33.457Z
2015-04-17T11:37:33.457Z
carlcs
<p>I have a date field, and I want it to be possible for this to be empty. How it works now is that if you don't enter a value, it takes today's date. Any suggestions?</p>

<datetime>
Empty date field becomes today's date
# Empty date field becomes today's date

(Asked by: username_0 on 2015-04-17)

I have a date field, and I want it to be possible for this to be empty. How it works now is that if you don't enter a value, it takes today's date. Any suggestions?

**Tags:** datetime


---

## 3 answers

### Answer by username_4 on 2015-04-17. Score: 8 | Accepted answer

That happens because the date filter returns the current date for null values / empty strings.

If the value passed to the date filter is null, it will return the current date by default.

To work around this, you would add a conditional.
{% if entry.myDate %}
    Date: {{ entry.myDate|date('Y-m-d') }}
{% endif %}

Another way to do this is to use a ternary operator.
{{ entry.myDate ? entry.myDate|date('Y-m-d') }}

> **Comments:**
> **username_5**: for completeness `{{ entry.myDate ? entry.myDate|date('Y-m-d') : 'XYZ' }}`  

---

### Answer by username_1 on 2016-01-20. Score: 2

You can add a new filter to twig that returns an empty string if the date is null or '0000-00-00', or if not reverts to Twig's built in date filter.
Here I've called it 'dateInclEmpty' (trying to think of a better name), you can otherwise use it in the same way as the existing date filter.
$filter = function(Twig_Environment $env){
    return new Twig_SimpleFilter('dateInclEmpty', function ($date,$format,$timezone) use($env){
        if (is_null($date) || $date == '0000-00-00'){
            return "";
        }
        else {
            return twig_date_format_filter($env,$date,$format,$timezone);
        }
    });
};
$twig->addFilter($filter($twig));

(Thanks for the advice Lindsey, let's see if this is any better :-P)

> **Comments:**
> **username_2**: -1 Hacking the core is never a good idea :(  
> **username_1**: I've made it as a new filter instead, hopefully less hacky  
> **username_2**: Much better! :)  

---

### Answer by username_3 on 2018-07-30. Score: 1

For DateTime:
{{ entry.myDate =="0000-00-00 00:00:00" ? "" : entry.myDate|date('Y-m-d') }}

For Date :
{{ entry.myDate =="0000-00-00" ? "" : entry.myDate|date('Y-m-d') }}


---

