Calculate First Week Of The Month From A Particular Day In Date Picker?
For example if 1st June is coming on Thursday than Week-1 of June should be from 5th as it's a Monday. I want to disable alternate weeks in the calendar but the week should start f
Solution 1:
Steps
- Get the start of month with
.startOf('month')
, I used.add(1, 'month')
for pass to next month - Check if the date is a Monday using
.day()
that return you a number from 0 to 6 (Sunday - Saturday), so Monday is the number 1 - If it isn't a Monday add to date a week with
.add(1, 'week')
and set the day at a Monday using.day()
but this time pass at the method the number of week that you want set (1)
constMONDAY = 1;
let nextMonthStartDate = moment().add(1, 'month').startOf('month');
if (nextMonthStartDate.day() !== MONDAY) nextMonthStartDate = moment(nextMonthStartDate).add(1, 'week').day(MONDAY);
console.log(`The first complete date of ${nextMonthStartDate.format('MMMM')} start at the ${nextMonthStartDate.format('LLLL')}`);
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
This code write all first complete week of current year.
const numberOfMonth = 11;
const yearStartDate = moment().startOf('year');
constMONDAY = 1;
for (let i = 0; i <= 11; i++) {
let monthStartDate = moment(yearStartDate).add(i, 'month').startOf('month');
if (monthStartDate.day() !== MONDAY) monthStartDate = moment(monthStartDate).add(1, 'week').startOf('week').day(MONDAY);
console.log(`The first complete date of ${monthStartDate.format('MMMM')} start at the ${monthStartDate.format('LLLL')}`);
}
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Post a Comment for "Calculate First Week Of The Month From A Particular Day In Date Picker?"