Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - convert number to month name

I have a very simple problem but for some reason I can't find the answer for it.

label.forEach(function(value){
    months.push(value['month']);
    revenue.push(value['revenue']);
});

The label is an array of numbers and revenue, in my case this is

[
{month: 9, revenue: 400}, 
{month: 11, revenue: 500},
{month: 12, revenue: 600}
]

This is a forEach loop in javascript, it pushes revenue and a month number into two seperate arrays, the problem is that the month is a number (e.g. 12) but I want the .push() to push a month name instead (December), I can't seem to find anything so I was hoping anyone here could help me out.

like image 922
peaceduck Avatar asked Apr 19 '18 07:04

peaceduck


People also ask

How do I convert month number to month name in react?

This below function takes the month number (like 1 or 2) as an argument and returns the month name in string format. Let's learn how the above function works: First we initialized a JavaScript new Date() constructor. We invoked a setMonth() method on JavaScript date Object by passing the month-1 as an argument.

How do I convert a month number to a month name in Excel?

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.


2 Answers

Intl.DateTimeFormat('en', { month: 'long' }).format(new Date('1')); // January

Can be achieved using Internationalization API

like image 152
Viktor Gusev Avatar answered Sep 28 '22 02:09

Viktor Gusev


var months = [ "January", "February", "March", "April", "May", "June", 
           "July", "August", "September", "October", "November", "December" ];

var selectedMonthName = months[value['month']];

look at the links

stack 1

like image 22
Oğuz Avatar answered Sep 28 '22 01:09

Oğuz