2

I have a content type with two "date and time" fields, configured to display in "22 November 2012 5:19pm" format:

date fields in Drupal

On the item itself, the start and end dates are both set to include times:

saved start and end date in Drupal

In my node.tpl.php, I output the two fields:

print drupal_render(field_view_field('node', $node, 'field_date'));
print drupal_render(field_view_field('node', $node, 'field_end_datetime'));

However, the two fields are displayed without the time:

Start Date and Time: 02 December 2012

End Date and Time: 02 December 2012

How do I output these two fields using my custom date format?

4

In the node template you should be able to output your dates with your selected format by doing:

print render($content['field_date']);
print render($content['field_end_datetime']);
4

If you use field_view_field for formatting date, pass $display parameter as defined below:

$display = array('settings' => array('format_type' => 'medium'));
$output = field_view_field('node', $node, 'field_date_time', $display);

format_type - is any valid date-time format name, defined in admin/config/regional/date-time of your drupal site.

  • I wonder how to strip all rendering html and label, just showing plain output only. – Adamtan Dec 26 '14 at 4:23
1

You may need to use field_view_value() to just show the formatted value.

Similar question which may be of use here:

How do I render a field value including its format?

  • Hi Kate: I tried $fields = field_get_items('node', $node, 'field_date'); $output = field_view_value('node', $node, 'field_date', $fields[0]); print render($output);, but that prints the same string as before (missing the time), just also missing the label. – George Nov 22 '12 at 8:09
1

I used this to create and render the date without a label.

$display = array('settings' => array('format_type' => 'medium'),'label'=>'hidden');
$output = field_view_field('node', $node, 'field_date', $display);
print render($output);

Then you can subsitute the machine name for your custom date format where it says 'medium'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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