Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

formatting dates in gridfield


Go to End


3 Posts   1479 Views

Avatar
Guy Van Bael

Community Member, 61 Posts

19 December 2014 at 8:57am

Hi,

I'm having a date in a gridfield's setdisplayfields. However it's displaying the date in the Y-m-d format. I'd like to format it to d-m-Y.
Can anybody help me out?
This is the piece of code i'm talking about.

$config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'StartDatum' => 'StartDatum',
));

Avatar
camfindlay

Forum Moderator, 267 Posts

22 December 2014 at 10:49am

Hey Guy,

You can actually set the date format using SilverStripe's date helpers and the $summary_fields static in the Model/DataObject.
See http://beta.docs.silverstripe.org/en/developer_guides/model/scaffolding/#summary-fields

For example:

private static $db = array(
    "StartDatum" => "Date"
);

private static $summary_fields = array(
    "StartDatum.Nice" => "Start date"
);

StartDatum.Nice will return the dd/mm/yy format and the "Start date" is the label that will be used in the GridField to name the column.

Avatar
Guy Van Bael

Community Member, 61 Posts

5 February 2015 at 9:00pm

Worked like a charm!
thanks Cam