P. Porto P. Porto - 2 months ago 10x
jQuery Question

converting month number to name jQuery

I have a question about something that i've searched a lot on the internet but can't seem to find a answer that clears all my doubts.

I am working with a query that goes to DB, picks the values I want and then encode it to JSON. One of the values is the MONTH. I save the month as a NUMBER so I can make my loops and conditions about it.

Everything is working fine except this:

I use the MONTH as a title in frontend. Is there anyway I can make a loop that converts the numbers (1 to 12) to strings (month names?).

$j('#container h2:last').html(data[i].monthName);

In html it appears like the month number.


Create a function that returns the month name from the month number like

function GetMonthName(monthNumber) {
      var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
      return months[monthNumber - 1];

And call it while populating the HTML like

$j('#container h2:last').html(GetMonthName(data[i].monthName));

There might be a lot of options . It's a logical approach so try first ;)