How to render only the Year when using date picker from OctoberCMS backend?

Ad

I’m using the date picker from the backend, and I’d like to display only the year that the user has selected. For example, instead of rendering 23-12-2017 on the DOM when is use {{ record.year }}, I’d only like to render the year 2017. How do I do this ? Is there a filter or something ?

Ad

Answer

Ad

You can use the date filter for Twig:

{{ record.date|date("Y") }}

By the way, if you are only using the year part of the date, you should also only save the year to the database, not the whole date. This would also mean you wouldn't have to format it in the first place. And if you need to save the whole date, please consider naming it something other than just year :)

Ad
source: stackoverflow.com
Ad