<p>The values entered in Date/Time fields are always saved as Craft datetime objects. This means it doesn't matter if you configure it to show the date, the time or both, Craft always saves them the same.</p>

<p>If you make it to show both, so you're able to enter a time for your events, you have to do this in the field's settings for all entries of your "Event" entry type. So to be able to switch input forms (a truly "optional" time field), you would actually need to have two entry types "Event with date" and "Event without date". Not the cleanest solution in my opinion.</p>

<p>What I'd do instead is to use <strong>two separate Date/Time fields</strong>, one configured to show the date and the other one to show the time. The client would only fill out the time field when necessary. And in the template you'd be able to check for an empty field.</p>

<pre><code>{{ entry.eventDate|date('l, j. F Y') }}

{% if entry.eventTime %}
    {{ entry.eventTime|date('G:i') }}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-03-04T19:42:54.257Z
That's a nice way of doing it! If the thing I'm trying to do isn't possible - which from what you say I guess it isn't - this would definitely be my next choice. One minor point: it's not quite right when you say that you *have* to enter a time - you can leave the time field blank. This was what led me to think I might be able to check the presence or absence of the time field, but in practice it seems a blank time just gets treated as 12:00am.
Nick F
CC BY-SA 3.0
2015-03-04T20:16:37.050Z
@NickF yes, it get's treated as 0:00. And based on my observations data only entered into the date form of a Date/Time field, with the time form left blank, end up showing 0:00 after saving the entry. Maybe some locale specialty, strange!?
carlcs
CC BY-SA 3.0
2015-03-03T19:13:31.430Z
2015-03-03T19:22:03.210Z
2015-03-03T19:22:03.210Z
carlcs
<p>Not from <strong>entry.dateField</strong> directly, but you can easily use the <a href="http://buildwithcraft.com/classreference/services/FieldsService" rel="nofollow">FieldsService</a> to pull the field's model and settings, and set a couple of true/false variables.</p>

<p>The following assumes your date field has the handle "dateField":</p>

<p>Try this (outside your craft.entries loop!):</p>

<pre><code>{% set dateField = craft.fields.getFieldByHandle( 'dateField' ) %}

{% set showDateFieldTime = dateField and dateField.settings.showTime %}
{% set showDateFieldDate = dateField and dateField.settings.showDate %}
</code></pre>

<p>Then, inside your entries loop:</p>

<pre><code>{% for entry in craft.entries.section( 'events' ).find() %}
   {% if entry.dateField and showDateFieldDate %}&lt;span class="date"&gt;{{ entry.dateField | date( 'M j, Y' ) }}&lt;/span&gt;{% endif %}
   {% if entry.dateField and showDateFieldTime %}&lt;span class="time&gt;{{ entry.dateField | date( 'h:i:s' ) }}{% endif %}
{% endfor %}
</code></pre>

<p>Note: solution is not tested.</p>

CC BY-SA 3.0
2015-03-04T12:55:46.830Z
Thanks, but the situation I'm talking about is the one where both date and time are settable (ie. time is shown but *could* be blank). Unless I've misunderstood, this would allow you to see whether or not time is shown, but not whether it is *set*, given that it is shown.
Nick F
CC BY-SA 3.0
2015-03-04T12:56:21.010Z
...this is still useful stuff to know, though!
Nick F
CC BY-SA 3.0
2015-03-04T12:58:50.580Z
Sorry, I misinterpreted your question – carlcs' answer using two different date fields should be a good solution, though :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-03-04T19:44:11.530Z
Ah no problem, thanks anyway. I still learnt something from your answer! :)
Nick F
CC BY-SA 3.0
2015-03-03T18:43:04.963Z
2015-03-03T19:06:12.770Z
2015-03-03T19:06:12.770Z
Mats Mikkel Rummelhoff
<p>Date fields can have an optional "time" box. This works nicely in the admin, but I can't figure out whether there's any way to access date and time separately when accessing that field via a template.</p>

<p>The use case I'm thinking of is an "Event Date" field on an "Event" entry type, which may or may not have a time (because an event may be all day, or may be at a particular time). The way you'd format the date would likely differ depending on whether the time field was filled in or not.</p>

<p>I realise there are other ways of achieving this effect (eg. adding an extra "show time" switch), but I'm wondering specifically whether this is currently doable using just a standard Date field's optional time field?</p>

<templating><datetime><fieldtypes>
Is there any way of distinguishing whether a Date field's "time" is set or not?
# Is there any way of distinguishing whether a Date field's "time" is set or not?

(Asked by: username_0 on 2015-03-03)

Date fields can have an optional "time" box. This works nicely in the admin, but I can't figure out whether there's any way to access date and time separately when accessing that field via a template.

The use case I'm thinking of is an "Event Date" field on an "Event" entry type, which may or may not have a time (because an event may be all day, or may be at a particular time). The way you'd format the date would likely differ depending on whether the time field was filled in or not.

I realise there are other ways of achieving this effect (eg. adding an extra "show time" switch), but I'm wondering specifically whether this is currently doable using just a standard Date field's optional time field?

**Tags:** templating, datetime, fieldtypes


---

## 2 answers

### Answer by username_1 on 2015-03-03. Score: 4 | Accepted answer

The values entered in Date/Time fields are always saved as Craft datetime objects. This means it doesn't matter if you configure it to show the date, the time or both, Craft always saves them the same.
If you make it to show both, so you're able to enter a time for your events, you have to do this in the field's settings for all entries of your "Event" entry type. So to be able to switch input forms (a truly "optional" time field), you would actually need to have two entry types "Event with date" and "Event without date". Not the cleanest solution in my opinion.
What I'd do instead is to use two separate Date/Time fields, one configured to show the date and the other one to show the time. The client would only fill out the time field when necessary. And in the template you'd be able to check for an empty field.
{{ entry.eventDate|date('l, j. F Y') }}

{% if entry.eventTime %}
    {{ entry.eventTime|date('G:i') }}
{% endif %}

> **Comments:**
> **username_0**: That's a nice way of doing it! If the thing I'm trying to do isn't possible - which from what you say I guess it isn't - this would definitely be my next choice. One minor point: it's not quite right when you say that you *have* to enter a time - you can leave the time field blank. This was what led me to think I might be able to check the presence or absence of the time field, but in practice it seems a blank time just gets treated as 12:00am.  
> **username_1**: @NickF yes, it get's treated as 0:00. And based on my observations data only entered into the date form of a Date/Time field, with the time form left blank, end up showing 0:00 after saving the entry. Maybe some locale specialty, strange!?  

---

### Answer by username_2 on 2015-03-03. Score: 1

Not from entry.dateField directly, but you can easily use the FieldsService to pull the field's model and settings, and set a couple of true/false variables.
The following assumes your date field has the handle "dateField":
Try this (outside your craft.entries loop!):
{% set dateField = craft.fields.getFieldByHandle( 'dateField' ) %}

{% set showDateFieldTime = dateField and dateField.settings.showTime %}
{% set showDateFieldDate = dateField and dateField.settings.showDate %}

Then, inside your entries loop:
{% for entry in craft.entries.section( 'events' ).find() %}
   {% if entry.dateField and showDateFieldDate %}{{ entry.dateField | date( 'M j, Y' ) }}{% endif %}
   {% if entry.dateField and showDateFieldTime %}<span class="time>{{ entry.dateField | date( 'h:i:s' ) }}{% endif %}
{% endfor %}

Note: solution is not tested.

> **Comments:**
> **username_0**: Thanks, but the situation I'm talking about is the one where both date and time are settable (ie. time is shown but *could* be blank). Unless I've misunderstood, this would allow you to see whether or not time is shown, but not whether it is *set*, given that it is shown.  
> **username_0**: ...this is still useful stuff to know, though!  
> **username_2**: Sorry, I misinterpreted your question – username_1' answer using two different date fields should be a good solution, though :)  
> **username_0**: Ah no problem, thanks anyway. I still learnt something from your answer! :)  

---

