Html 如何使用 Twig (Symfony) 将时间格式化为 hh:mm
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27838350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I format time to hh:mm using Twig (Symfony)
提问by Kheran
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).
我从我的 MS SQL 数据库中检索了一个时间字段,例如“10:30:00”(hh:mm:ss)。我尝试在树枝模板中渲染它,但我只想显示“10:30”部分(hh:mm)。
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:
我尝试使用 number_format 和 date_format 完成此操作,但似乎无法完成。例如,失败的尝试将是:
<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.
是的,这没有意义。但我找不到任何与我想要的东西相距甚远的东西——我想我忽略了一些东西。
Thanks
谢谢
回答by Xavier Norbal
You can use the Twig filter date like this :
您可以像这样使用 Twig 过滤器日期:
{{ object.date|date('H:i:s') }}
As seen on this thread : How to render a DateTime object in a Twig template