Formating dates in Gridfield


#1

Silverstripe Version: 4.3.3

Question: Ist is possible to set the format of a date on an GridFields column?

I have a ModelAdmin for my News. News has date as Column. In summary_fields. I could use make a function like getDateNice and use it instead of date in summary_fields. But when I do GridfieldFilterHeader won’t work.
So is it possible to format a DBDate in Gridfield to d.m.Y Format without loosing the advantages of GridfieldFilterHeader and GridFieldSortableHeader


private static $summary_fields = array(
	'Headline', "URLSegment", 'date'
);

A Datepicker inside the GridFieldFilterHeader would be awesome.


#2

It should be possible but may be inconvenient and not documented anywhere. This is SS3 example code so might need updating.

// Make gridfield values nice to read while preserving sorting
$columns = $config->getComponentByType('GridFieldDataColumns');
$columns->setFieldCasting(array(
	'Enabled' => 'Boolean->Nice',
	'Created' => 'Date->Nice',
));

I think we really need an easy way to do this. I love this simple solution for the problem but it hasn’t gone anywhere yet to my knowledge.


#3

Thanks. This is working. so I’ve d.m.Y format fields in the List. But when I search I’ve to type Y-m-d. No searching for d.m.Y possible.