How to display month name by number?

China☆狼群 提交于 2020-04-08 08:56:55

问题


I have a number represented month and i want to replace it with month name, date filter not working:

{{ monthnumber|date:"M" }}

I want to place two links - next month and previous month, but i have only number of month.

How to do it?


回答1:


You'll need a custom template filter to convert the number into a month name. Pretty simple if you use the calendar module from the standard library:

import calendar

@register.filter
def month_name(month_number):
    return calendar.month_name[month_number]

Usage example: {{ 5|month_name }} will output May




回答2:


you should use :

{{ datetimeobj|date:"F" }}

read this




回答3:


Well, it's not :

{{ monthnumber|date:"M" }}

You should try this :

{{ datetimeobj|date:"M" }}



回答4:


Just in case you do not want to format a date object passed into a Django template (like the other answers clearly show), but rather you'd prefer to simply get the current date's month number; here is the built-in tag you can use to do that:

{% now "n" %}

From the docs: https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#now

Here are the built in date tags you can use with {% now [""] %}

https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#date



来源:https://stackoverflow.com/questions/7385751/how-to-display-month-name-by-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!