14

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).

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.

Thanks

Kheran
  • 552
  • 3
  • 7
  • 21

1 Answers1

36

You can use the Twig filter date like this :

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

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

Community
  • 1
  • 1
Xavier Norbal
  • 476
  • 4
  • 8
  • 1
    Please note it works only for time going from 00:00:00 to 23:59:00. If time is a period longer than 24 hours, it will not works. – lepix Dec 26 '16 at 15:07
  • `h` instead of `H` for 12 hours – TRiNE Mar 02 '17 at 13:51
  • 1
    if time is null then it will show the current time, do this trick `{{ object.myTime ? object.myTime|date('H:i:s') }}` – habibun Apr 07 '19 at 07:09
  • When I do this on a unix date (or a number of seconds really, just want to format them as hour:minute) I get an additional hour on everything. Anything to do with my LOCALE? so a value of 300 should be formatted as 00:05 but instead becomes 01:05. Consistently over all my entities... – Matt Welander Aug 31 '21 at 11:01