JSFiddle - React, Tailwind, and code Playground

by ArsenMelkumiants

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

JavaScript

const vm = {};

vm.startMonth = '2017-03';
vm.endMonth = '2018-12';

vm.totalMonths = moment(vm.endMonth).diff(vm.startMonth, 'months'); // => 21 monts
vm.totalYears = Math.ceil(vm.totalMonths / 12); // => 2 year

vm.columnLabels = {};

let startDate = moment(vm.startMonth);

for (let i = 0; i <= vm.totalMonths; i++) {
	const year = startDate.format('YYYY');
  const month = startDate.format('MMM');
	
  vm.columnLabels[year] = vm.columnLabels[year] || [];
  vm.columnLabels[year].push(month);

  startDate.add(1, 'months');
}