Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display month name by number?

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?

like image 666
Nips Avatar asked Sep 12 '11 09:09

Nips


People also ask

How do you convert a number to a month name?

Please do as follows: Select a blank cell next to the sales table, type the formula =TEXT(A2*29,"mmm") (Note: A2 is the first number of the Month list you will convert to month name), and then drag the AutoFill Handle down to other cells. Now you will see the numbers (from 1 to 12) are converted to normal month names.

How can get month name from month number in SQL?

To convert month number to month name we have to use a function MONTHNAME(), this function takes date column or a date as a string and returns the Month name corresponding to the month number.


1 Answers

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

like image 136
Daniel Roseman Avatar answered Sep 20 '22 10:09

Daniel Roseman