Converting month number to month name
I solved it using the date utils in views.py, as:
'usage_date': date(int(year), int(month), 1)
And then in the template, rendered the month names using Django shortcuts.
This works well if I want the month name to change in accordance with the language chosen.
Convert month's number to Month name
We can just use month.name
to change the index of months to its corresponding name
month.name[v1]
and there is also a 3 character abbreviation for month
month.abb[v1]
data
set.seed(24)
v1 <- sample(1:12, 24, replace = TRUE)
python/pandas: convert month int to month name
You can do this efficiently with combining calendar.month_abbr
and df[col].apply()
import calendar
df['Month'] = df['Month'].apply(lambda x: calendar.month_abbr[x])
MySQL convert month number to month name
- We can convert the given input number to MySQL date format (focusing on month only), using
Str_To_Date()
function. - Now, we simply need to use
Monthname()
function to extract the month name from the date. - This will work only when
NO_ZERO_DATE
mode is disabled.
Try:
SET sql_mode = ''; -- disable NO_ZERO_DATE mode
SELECT MONTHNAME(STR_TO_DATE(1, '%m'));
As @Felk suggested in comments, if we need to get shortened month name, we can use Date_Format()
function instead:
SET sql_mode = ''; -- disable NO_ZERO_DATE mode
SELECT DATE_FORMAT(STR_TO_DATE(1, '%m'), '%b');
If you don't want to disable the NO_ZERO_DATE
mode, then you can create any random date using the month and call Monthname()
:
SELECT MONTHNAME(CONCAT('2018-',3,'-1')); -- 3 is the input number
Get month name from number
Calendar API
From that you can see that calendar.month_name[3]
would return March
, and the array index of 0
is the empty string, so there's no need to worry about zero-indexing either.
Convert Month Number to Month Name Function in SQL
A little hacky but should work:
SELECT DATENAME(month, DATEADD(month, @mydate-1, CAST('2008-01-01' AS datetime)))
How to map month name to month number and vice versa?
Create a reverse dictionary using the calendar
module (which, like any module, you will need to import):
{month: index for index, month in enumerate(calendar.month_abbr) if month}
In Python versions before 2.7, due to dict comprehension syntax not being supported in the language, you would have to do
dict((month, index) for index, month in enumerate(calendar.month_abbr) if month)
Convert month int to month name
CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1);
See Here for more details.
Or
DateTime dt = DateTime.Now;
Console.WriteLine( dt.ToString( "MMMM" ) );
Or if you want to get the culture-specific abbreviated name.
GetAbbreviatedMonthName(1);
Reference
Related Topics
Finding If Boolean Is Ever True by Groups in R
Transform One Column from Categoric to Binary, Keep the Rest
R - Random Forest and More Than 53 Categories
R Cmd Check Not Looking for Gcc in Rtools Directory
Generate Id for Each Group with Repeated and Missing Observations
How to Subset Column Variables in Df1 Based on the Important Variables I Got in Df2
Add Multiple Curves/Functions to One Ggplot Through Looping
How to Decode Postgresql Bytea Column Hex to Int16/Uint16 in R
Aggregating Monthly Column Values into Quarterly Values
Creating a Prng Engine for <Random> in C++11 That Matches Prng Results in R
Converting Multiple Boolean Columns to Single Factor Column
Control Padding of Grobs Added to Patchwork
Replacing for Loop with Foreach Loop
How to Filter Rows Based on the Previous Row and Keep Previous Row Using Dplyr
Removing Row with Duplicated Values in All Columns of a Data Frame (R)