Problem/Motivation
If you have a date variable in Twig you can't format it using date formats you've created in Drupal. Twig has a date filter but it does not hook up to Drupal things.
Beta phase evaluation
Proposed resolution
Add a new filter to do this.
Before
In preprocess or the render array variables.
// Functional service wrapper from D7.
format_date($timestamp, 'html_date');
// Date Formatter Service
\Drupal::service('date.formatter')->format($timestamp, 'html_date');
After
{{ timestamp|format_date('html_date') }}
Also note that Twig has a date filter & function already, but this doesn't work with the named formats defined in the Drupal UI or system
http://twig.sensiolabs.org/doc/filters/date.html
{{ post.published_at|date("m/d/Y") }}
Remaining tasks
TBD
User interface changes
n/a
API changes
API addition, ability to use Drupal date formats from Twig templates.
Data model changes
None.
Comments
Comment #2
joelpittetComment #3
lauriiiComment #4
lauriiiCreated follow-up for this: #2571633: Remove TwigExtension setter methods - use constructor instead
Comment #5
star-szrUse \Drupal\Core\Template\Loader\StringLoader instead here, \Twig_Loader_String() is going away. See #2568181: [META] Update to Twig 2.x in Drupal 9.
That's all I found, thanks @lauriii!
Comment #6
lauriiiThanks for the review @Cottser!
Comment #7
joelpittetChanged the filter name to match the old drupal function name. date_format back to format_date.
This has tests and seems to be a nice solution to have this in the drupal twig extension, may allow for less preprocessing.
Thank you @lauriii and @Cottser
Comment #8
star-szrHere's a usage, this shows how this can help us to simplify preprocess in some cases.
Comment #10
joelpittetWe missed a couple things. This should resolve those things. Like having a usecase to show how this fixes unnecessary preprocess! +1 to that.
Comment #12
joelpittetRandom CI fail.
Comment #13
lauriiiThe changes @Cottser and @joelpittet made after #7 looks sane.
Comment #14
Anonymous (not verified) commentedI did some manual testing using an inline template render array, and this works as expected.

So RTBC+1 from me.
Comment #15
alexpottThis makes lots of sense. I'm committing this under the committer discretion clause in the beta policy. Committed b0f2f83 and pushed to 8.0.x. Thanks!