How to format DateTime on Twig

 


In above video explain how to deal with DateTime Object on twig page by using date filter.

In twig we have “date” filter to print the date and format the date time.

Lets learn it from some examples

Print the current date and time with mysql date format on twig page

{{ “now” | date(‘Y-m-d H:i:s’) }}

Print current year on twig page

{{ “now” | date(‘Y’) }}

Print the only date on twig page

{{ “now” | date(‘d-m-Y’) }}

Print the current time on twig page

{{ “now” | date(‘H:i:s’) }}

Create macro to print the date and time in mysql format, using macro we can create reusable components.

{# file — datetime_test/date_time_macro.html.twig #}

Comments