JSFiddle - React, Tailwind, and code Playground

by HemalathaThanigaivel

HTML

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

JavaScript

function log (msg) {
    $('body').append('<p>' + msg);
}

function sameMonth (a, b, other) {
		if (a.month() !== b.month()) {
		        return other;
		    }
    return a.format('DD/MM/YYYY');
}

function weeks (m1, m2) {
		let getDay = m1.day();
    var lastOfMonth     = m2,
        lastOfMonthDate = lastOfMonth.date(),
        firstOfMonth    = m1,
        fDate = firstOfMonth.date(),
        currentWeek     = firstOfMonth.clone().day(getDay),
        output          = [],
        startOfWeek,
        endOfWeek;
	let i = 0, j;
    while (currentWeek < lastOfMonth) {
    		j = i == 0 ? getDay : 0;
        startOfWeek = sameMonth(currentWeek.clone().day(j), firstOfMonth, 1);
        endOfWeek = sameMonth(currentWeek.clone().day(6), firstOfMonth, lastOfMonthDate);
        console.log(startOfWeek ,endOfWeek)
       if(moment(endOfWeek, 'DD/MM/YYYY').isAfter(moment(lastOfMonth))) {
        	console.log('here comes')
          output.push(startOfWeek + '-' + moment(lastOfMonth).format('DD/MM/YYYY'));
        }else{
        	output.push(startOfWeek + '-' + endOfWeek);
        }
        currentWeek.add(7, 'd');
        i++;
    }
    
    return output;
}

log(weeks(moment('2019-02-01', 'YYYY-MM-DD'), moment('2019-02-28', 'YYYY-MM-DD')).join(' , '));