jinja2 how to remove microsecond in datetime

前端 未结 2 1493
逝去的感伤
逝去的感伤 2021-01-22 11:42

In a Jinja2 template I want to display the last login:

Last Login: {{ user.last_seen }}

last_seen is supposed to be a datetime obj

2条回答
  •  清酒与你
    2021-01-22 11:58

    You are using the default string formatting of a datetime object, which is essentially the same as calling datetime.isoformat(' '), a format that includes the microseconds component.

    If you want a different format, then do so explicitly, using the datetime.datetime.strftime() method:

    Last Login: {{ user.last_seen.strftime('%Y-%m-%d %H:%M:%S') }}
    

    Alternatively, produce a new datetime object with the microseconds component set to 0, then interpolate that:

    Last Login: {{ user.last_seen.replace(microsecond=0) }}
    

提交回复
热议问题