How to format dateTime in django template?

后端 未结 4 1680
情书的邮戳
情书的邮戳 2020-12-25 09:53

That:

{{ wpis.entry.lastChangeDate|date:\"D d M Y\" }}

gives me (why?):

2009-07-24 21:45:38.986156 

and

相关标签:
4条回答
  • 2020-12-25 09:57

    You can use this:

    addedDate = datetime.now().replace(microsecond=0)

    0 讨论(0)
  • 2020-12-25 10:04

    This is exactly what you want. Try this:

    {{ wpis.entry.lastChangeDate|date:'Y-m-d H:i' }}
    
    0 讨论(0)
  • 2020-12-25 10:13

    {{ wpis.entry.lastChangeDate|date:"SHORT_DATETIME_FORMAT" }}

    0 讨论(0)
  • 2020-12-25 10:20

    I suspect wpis.entry.lastChangeDate has been somehow transformed into a string in the view, before arriving to the template.

    In order to verify this hypothesis, you may just check in the view if it has some property/method that only strings have - like for instance wpis.entry.lastChangeDate.upper, and then see if the template crashes.

    You could also create your own custom filter, and use it for debugging purposes, letting it inspect the object, and writing the results of the inspection on the page, or simply on the console. It would be able to inspect the object, and check if it is really a DateTimeField.

    On an unrelated notice, why don't you use models.DateTimeField(auto_now_add=True) to set the datetime on creation?

    0 讨论(0)
提交回复
热议问题