1

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?

2 Answers 2

4

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 %}
2
  • 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
    Mar 4, 2015 at 19:42
  • @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
    Mar 4, 2015 at 20:16
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 %}<span class="date">{{ entry.dateField | date( 'M j, Y' ) }}</span>{% endif %}
   {% if entry.dateField and showDateFieldTime %}<span class="time>{{ entry.dateField | date( 'h:i:s' ) }}{% endif %}
{% endfor %}

Note: solution is not tested.

4
  • 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
    Mar 4, 2015 at 12:55
  • ...this is still useful stuff to know, though!
    – Nick F
    Mar 4, 2015 at 12:56
  • Sorry, I misinterpreted your question – carlcs' answer using two different date fields should be a good solution, though :) Mar 4, 2015 at 12:58
  • Ah no problem, thanks anyway. I still learnt something from your answer! :)
    – Nick F
    Mar 4, 2015 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.