如何使用 Twig (Symfony) 将时间格式化为 hh:mm

2022-01-22 00:00:00 symfony html twig

我从我的 MS SQL 数据库中检索一个时间域,例如10:30:00"(hh:mm:ss).我尝试在树枝模板中呈现它,但我只想显示10:30"部分(hh:mm).

I retrieve a timefield from my MS SQL database, for example '10:30:00' (hh:mm:ss). I try to render this in a twig template, but I only want to display the '10:30' portion (hh:mm).

我尝试使用 number_format 和 date_format 来完成这项工作,但我似乎无法完成.例如,失败的尝试是:

I've tried to get this done using both number_format and date_format, but I can't seem to get it done. For example, a failed attempt would be:

<td class="PODTIME">{{ record.PODTIME|number_format(2, ':') }}</td>

是的,这没有意义.但我找不到任何与我想要的东西很接近的东西——我想我忽略了一些东西.

Yeah that doesn't make sense. But I can't find anything even remotely close to what I want - I guess I'm overlooking something.

谢谢

推荐答案

你可以像这样使用 Twig 过滤日期:

You can use the Twig filter date like this :

{{ object.date|date('H:i:s') }}

正如在这个线程上看到的:如何渲染Twig 模板中的 DateTime 对象

As seen on this thread : How to render a DateTime object in a Twig template

相关文章