3

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.

abdul qayyum
  • 535
  • 1
  • 17
  • 39
bugfire
  • 67
  • 1
  • 8

2 Answers2

12

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 ;)

Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
0

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

$('.date').each(function(){

 var dateStr = $(this).text().split('/'),
  dateDD = dateStr[0],
  dateMM = dateStr[1],
  dateYY = dateStr[2];

 function GetMonthName(monthNumber) {

  var months = ['Jan', 'Feb', 'Mar', 'Avr', 'Mai', 'Juin', 'Juil', 'Août', 'Sept', 'Oct', 'Nov', 'Dec'];
  return months[monthNumber - 1];

 }

 var newDate = dateStr[0] + ' <b>' + GetMonthName(dateMM) + '</b> ' + dateStr[2];
 $(this).html(newDate);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<p class="date">19/10/2018</p>
Simo OM
  • 139
  • 6