JSFiddle - React, Tailwind, and code Playground

by Tom Randolph

HTML

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

JavaScript

function* getTwelveMonths(){
	var thisMonth = moment()
        .startOf( "month" );
    var oneYearAgo = moment( thisMonth )
        .subtract( { "years": 1 } );

    var start = moment( oneYearAgo );
    var end = moment( start ).endOf( "month" );
    var now = moment();

    for( let i = 0; i < 12; i++ ){
        start = moment( start ).add( { "months": 1 } );
        end = moment( start ).endOf( "month" );

        end = moment.min( end, now );

        yield { start, end };
    }
}

var twelveMonths = getTwelveMonths();

console.log( getTwelveMonths );
console.log( twelveMonths );

for( let month in twelveMonths ){
	console.log( month );
}