<p>This makes sense. The output of "diff" is a date object and you are requesting the days of the formatted date (03-15-0000), in this case 15 days.</p>

<p>Your best bet is to extract the days from the object rather than from the formatted date.
Try...</p>

<p><code>
{{ inschrijven.datumEvenement.diff(now).days }}
</code></p>

CC BY-SA 3.0
2015-05-15T13:09:19.947Z
2015-05-15T13:09:19.947Z
Jake Cattrall
<p>This will work if you want to diff from <code>now</code>.</p>

<pre><code>{% set leftDays = meet.dateStart.diff( date("now") ).format('%a') %}
</code></pre>

CC BY-SA 3.0
2016-08-20T11:13:23.690Z
2016-08-22T08:51:31.377Z
2016-08-22T08:51:31.377Z
Developer
<p>You want to use "a" as your <a href="http://php.net/DateInterval.format" rel="noreferrer">DateInterval format parameter</a> instead of "d".</p>

<p>Also, you may need to use <a href="http://twig.sensiolabs.org/doc/functions/date.html" rel="noreferrer">Twig's date function</a> on the end date, which "converts an argument to a date to allow date comparison".</p>

<p>This should give you the correct number of days between now and the future date:</p>

<pre><code>{{ date( inschrijven.datumEvenement|date ).diff( now ).format('%a') }}
</code></pre>

<p>or</p>

<pre><code>{{ date( inschrijven.datumEvenement|date ).diff( now ).days }}
</code></pre>

CC BY-SA 3.0
2015-05-15T19:12:02.763Z
Dylan thanks, your suggestion did the job! 106 days to go..
Robbie Post
CC BY-SA 3.0
2015-05-15T21:32:12.490Z
@Robbie if `inschrijven.datumEvenement` is from a Date/Time field than it is already a DateTime object and you don't need the `date` filter or `date` function. But even if it is not, you never need both! :D
carlcs
CC BY-SA 3.0
2015-05-15T21:56:00.307Z
@carlcs I thought so too but I was seeing incorrect results without the date function. For example `{{ globalSet.dateField.diff( now ).format('%a') }}` resulted in `(unknown)` and `{{ globalSet.dateField.diff( now ).days }}` outputs `-99999`. And using the date function without the date filter also produced the same results. Do you know why that would be?
Dylan T
CC BY-SA 3.0
2015-05-15T22:11:39.860Z
Hmm.. it could be possible that Craft DateTime objects don't do the `diff` method or do something wrong, and the `date` filter converts them to normal Twig DateTime objects?
carlcs
CC BY-SA 3.0
2015-05-15T23:18:48.143Z
Ok, it is, as I guessed, a bug with the Craft DateTime object. Normal DateTime object you create using Twig's `date` function work just fine: `date('2015-05-01').diff(date('2015-05-04')).days`. What you actually do with combining `date` filter and function is to return the Craft DateTime as a string and then make that a normal Twig DateTime using the function. One thing to note to that "workaround" is that you should better return the DateTime including the time and the timezone with the filter, e.g. `date(myDate|date('c'))` or `date(myDate|date('U'))`.
carlcs
CC BY-SA 3.0
2015-05-15T23:35:30.853Z
The problem / bug only exists for the first argument (the one you call the `diff`method on), so `date(dt1|date('c')).diff(dt2).days` works with Craft DateTime objects.
carlcs
CC BY-SA 3.0
2015-05-15T13:11:54.207Z
2015-05-15T22:55:26.550Z
2015-05-15T22:55:26.550Z
Dylan T
<p>I want to calculate the remaining days between the current day (now) and a future date set as a date field in {{ inschrijven.datumEvenement }} globals</p>

<pre><code>{{ inschrijven.datumEvenement.diff(now).format('%d') }}
</code></pre>

<p>This is what I came up with but it doesn't generate the right number of days left. </p>

<p>Using now = 05-15-2015, inschrijven.datumEvenement = 08-30-2015 the above tag generates "14".</p>

<datetime><globals>
How to calculate the number of days left?
# How to calculate the number of days left?

(Asked by: username_0 on 2015-05-15)

I want to calculate the remaining days between the current day (now) and a future date set as a date field in {{ inschrijven.datumEvenement }} globals
{{ inschrijven.datumEvenement.diff(now).format('%d') }}

This is what I came up with but it doesn't generate the right number of days left. 
Using now = 05-15-2015, inschrijven.datumEvenement = 08-30-2015 the above tag generates "14".

**Tags:** datetime, globals


---

## 3 answers

### Answer by username_3 on 2015-05-15. Score: 8 | Accepted answer

You want to use "a" as your DateInterval format parameter instead of "d".
Also, you may need to use Twig's date function on the end date, which "converts an argument to a date to allow date comparison".
This should give you the correct number of days between now and the future date:
{{ date( inschrijven.datumEvenement|date ).diff( now ).format('%a') }}

or
{{ date( inschrijven.datumEvenement|date ).diff( now ).days }}

> **Comments:**
> **username_0**: Dylan thanks, your suggestion did the job! 106 days to go..  
> **username_4**: @Robbie if `inschrijven.datumEvenement` is from a Date/Time field than it is already a DateTime object and you don't need the `date` filter or `date` function. But even if it is not, you never need both! :D  
> **username_3**: @username_4 I thought so too but I was seeing incorrect results without the date function. For example `{{ globalSet.dateField.diff( now ).format('%a') }}` resulted in `(unknown)` and `{{ globalSet.dateField.diff( now ).days }}` outputs `-99999`. And using the date function without the date filter also produced the same results. Do you know why that would be?  
> **username_4**: Hmm.. it could be possible that Craft DateTime objects don't do the `diff` method or do something wrong, and the `date` filter converts them to normal Twig DateTime objects?  
> **username_4**: Ok, it is, as I guessed, a bug with the Craft DateTime object. Normal DateTime object you create using Twig's `date` function work just fine: `date('2015-05-01').diff(date('2015-05-04')).days`. What you actually do with combining `date` filter and function is to return the Craft DateTime as a string and then make that a normal Twig DateTime using the function. One thing to note to that "workaround" is that you should better return the DateTime including the time and the timezone with the filter, e.g. `date(myDate|date('c'))` or `date(myDate|date('U'))`.  
> **username_4**: The problem / bug only exists for the first argument (the one you call the `diff`method on), so `date(dt1|date('c')).diff(dt2).days` works with Craft DateTime objects.  

---

### Answer by username_1 on 2015-05-15. Score: 2

This makes sense. The output of "diff" is a date object and you are requesting the days of the formatted date (03-15-0000), in this case 15 days.

Your best bet is to extract the days from the object rather than from the formatted date.
Try...

`{{ inschrijven.datumEvenement.diff(now).days }}`


---

### Answer by username_2 on 2016-08-20. Score: 2

This will work if you want to diff from now.
{% set leftDays = meet.dateStart.diff( date("now") ).format('%a') %}


---

