List month name and year for the last 6 months - Javascript Date Operation

Javascript examples for Date Operation:Month

Description

List month name and year for the last 6 months

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <script>
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var today = new Date();/*from ww w .jav a  2s.  co m*/
var d;
var month;
for(var i = 6; i > 0; i -= 1) {
  d = new Date(today.getFullYear(), today.getMonth() - i, 1);
  month = monthNames[d.getMonth()];
  console.log(month);
}

      </script>  
   </body>
</html>

Related Tutorials