当前位置: 动力学知识库 > 问答 > 编程问答 >

django template changed date format from queryset

问题描述:

I created a queryset:

my_data = My_User.objects.filter(Q(first_name = 'John') |

Q(last_name = 'Doe'))

now one of the fields in my_data has date (type: datetime.datetime)

format of this date field in db is yyyy-mm-dd HH:MM:SS eg: 2013-04-01 12:50:58

but when I display this information via django template, date format changes by default!

It displays: April 1, 2013, 12:50 p.m.

code in template:

{% for each_user in my_data %}

<tr>

<td>{{each_user.timestamp}}</td>

<td>{{each_user.first_name}}</td>

<td>{{each_user.last_name}}</td>

<td>{{each_user.address}}</td>

</tr>

{% endfor %}

I want date format as: mm-dd-yyyy HH:MM:SS AM/PM.

any suggestions?

网友答案:

The default date format for django template is: 'N j, Y' (e.g. Feb. 4, 2003) Hence you see the different date format from the database. You can read more about this here

You can format date using the inbuilt date filter:

{{each_user.timestamp|date:"%d/%m/%y %H:%M"}}

You can replace the "%d/%m/%y %H:%M" with any format that you wish.

"%d/%m/%y %H:%M" gives you the format "21/11/06 16:30"

More info: here

分享给朋友:
您可能感兴趣的文章:
随机阅读: