Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions docs/templating/twig_function_reference/date_twig_filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,46 @@
``` html+twig
{{ content.contentInfo.publishedDate|ibexa_short_datetime('PST') }}
```

## Considerations for use outside the back office

The filters rely on user preferences.
When the preferences are not set, for example, for logged out users, the filters fall back to a default date format.
For some filters, the fallback date format includes locale-aware fragments, such as the full month or day name.

Check warning on line 33 in docs/templating/twig_function_reference/date_twig_filters.md

View workflow job for this annotation

GitHub Actions / vale

[vale] docs/templating/twig_function_reference/date_twig_filters.md#L33

[Ibexa.OxfordComma] Use a comma before the last 'and' or 'or' in a list of four or more items.
Raw output
{"message": "[Ibexa.OxfordComma] Use a comma before the last 'and' or 'or' in a list of four or more items.", "location": {"path": "docs/templating/twig_function_reference/date_twig_filters.md", "range": {"start": {"line": 33, "column": 1}}}, "severity": "WARNING"}
When combined with [reverse proxies like Varnish or Fastly](http_cache.md), it's possible to cache a localized version of a date and display it to other users, even if they're not using the same locale.

Consider these alternatives:

- Use Twig's built-in `date` filter with a fixed, locale-independent format

``` html+twig
{{ content.contentInfo.publishedDate|date('Y-m-d H:i:s') }}
```

- Use ESI for dynamic rendering of the date

``` html+twig
{{ render_esi(controller('App\\Controller\\CustomDateController::format', {
'date': content.contentInfo.publishedDate
})) }}
```

Don't cache the ESI response.
By implementing this solution, you can keep the date format locale-aware.

- Client-side JavaScript formatting
Comment thread
mnocon marked this conversation as resolved.

``` html+twig
<span data-datetime="{{ content.contentInfo.publishedDate|date('c') }}">{{ content.contentInfo.publishedDate|date('Y-m-d H:i:s') }}</span>
<script>
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('[data-datetime]').forEach(function(elem) {
var datetime = new Date(elem.getAttribute('data-datetime'));
elem.innerHTML = datetime.toLocaleString();
});
});
</script>
```

For more information, see [HTTP Cache](http_cache.md) and [Delivering personalized responses](context_aware_cache.md#personalize-responses).