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.date();
}

function weeks (m) {
    var lastOfMonth     = m.clone().endOf('month'),
        lastOfMonthDate = lastOfMonth.date(),
        firstOfMonth    = m.clone().startOf('month'),
        currentWeek     = firstOfMonth.clone().day(0),
        output          = [],
        startOfWeek,
        endOfWeek;

    while (currentWeek < lastOfMonth) {
        startOfWeek = sameMonth(currentWeek.clone().day(0), firstOfMonth, 1);
        endOfWeek = sameMonth(currentWeek.clone().day(6), firstOfMonth, lastOfMonthDate);
        
        output.push(startOfWeek + '-' + endOfWeek);
        currentWeek.add(7, 'd');
    }
    
    return output;
}

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




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

function sameMonth (a, b, other) {
    if (a.month() !== b.month()) {
        return other;
    }
    return a.date();
}

function weeks (m1, m2) {
let getDay = m1.day();
    var lastOfMonth     = m2,
        lastOfMonthDate = lastOfMonth.date(),
        firstOfMonth    = m1,
        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(getDay), firstOfMonth, 1);
        endOfWeek = sameMonth(currentWeek.clone().day(6), firstOfMonth, lastOfMonthDate);
        
        output.push(startOfWeek + '-' + endOfWeek);
        currentWeek.add(7, 'd');
    }
    
    return output;
}

log(weeks(moment('2019-02-01', 'YYYY-MM-DD'), moment('2019-02-28', 'YYYY-MM-DD')).join(' , '));
/* log(weeks(moment('2012-10-01', 'YYYY-MM-DD')).join(' , '));
log(weeks(moment('2012-11-01', 'YYYY-MM-DD')).join(' , '));
log(weeks(moment('2012-12-01',...