1

I am grabbing the date of a blog post from a page on a site and displaying it on another page on the same site.

The date reads 2/22/18 on the initial page and the jQuery code below successfully strips it to just "2" and appends it to the div with the "box-month-1" class on the second page. However, I want to change the value of that "2" to "FEB", but I can't get it to work. I'd appreciate any help anyone can offer! Thanks in advance.

HTML

<div class="box-month-1"></div>  

jQuery

$.get('/blog.html', function(data){   

  var s = $(data).find('.blog-date .date-text').text().split('/').slice(0,1).join('/');
  $('.box-month-1').append(s);

  var boxOne = $('.box-month-1').text();

  var jan = "JAN";
  var feb = "FEB";
  var mar = "MAR";

  if (boxOne.equals('2')) {
    $('.box-month').replaceWith(feb);
  }

});
Weebs
  • 155
  • 2
  • 10
  • Possible duplicate of [Get month name from Date](https://stackoverflow.com/questions/1643320/get-month-name-from-date) – Sebastian Simon Mar 28 '18 at 18:35
  • Let me see if I understand: you want to get the month part from the date, then append the initials of the month to the `.box-month-1`, right? – Phiter Mar 28 '18 at 18:39
  • @Phiter No, the code already does that. I want to change the appended number value of the month to text. So make "2" = "FEB". – Weebs Mar 28 '18 at 18:46

1 Answers1

1

Try this:

      $.get('/blog.html', function(data){   

      var s = $(data).find('.blog-date .date-text').text().split('/').slice(0,1).join('/');
      $('.box-month-1').append(s);

      var boxOne = $('.box-month-1').text();

      var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul","Aug","Sep", "Oct", "Nov", "Dec"]; 

        $('.box-month-1').html(months[parseInt(boxOne)+1]); 

    });
Supun Praneeth
  • 3,087
  • 2
  • 30
  • 33