Moment Js Get Number Of Weeks In A Month
Solution 1:
I have created this gist that finds all the weeks in a given month and year. By calculated the length of calendar
, you will know the number of weeks.
https://gist.github.com/guillaumepiot/095b5e02b4ca22680a50
# yearandmonthare variables
year=2015month=7 # August (0 indexed)
startDate = moment([year, month])
# Get the firstandlastdayof the month
firstDay = moment(startDate).startOf('month')
endDay = moment(startDate).endOf('month')
# Create a rangefor the month we can iterate through
monthRange = moment.range(firstDay, endDay)
# Getall the weeks during the currentmonth
weeks = []
monthRange.by('days', (moment)->
if moment.week() notin weeks
weeks.push(moment.week())
)
# Create a rangeforeach week
calendar = []
for week in weeks
# Create a rangefor that week between1st and7th day
firstWeekDay = moment().week(week).day(1)
lastWeekDay = moment().week(week).day(7)
weekRange = moment.range(firstWeekDay, lastWeekDay)
# Addto the calendar
calendar.push(weekRange)
console.log calendar
Solution 2:
Can be easily done using raw javascript:
functiongetNumWeeksForMonth(year,month){
date = newDate(year,month-1,1);
day = date.getDay();
numDaysInMonth = newDate(year, month, 0).getDate();
returnMath.ceil((numDaysInMonth + day) / 7);
}
You get the day index of the first day, add it to the number of days to compensate for the number of days lost in the first week, divide by 7 and use ceil to add 1 for the simplest overflow in the next week
Solution 3:
EDIT:
NEW and hopefully very correct implementation:
function calcWeeksInMonth(date: Moment) {
const dateFirst = moment(date).date(1);
const dateLast = moment(date).date(date.daysInMonth());
const startWeek = dateFirst.isoWeek();
const endWeek = dateLast.isoWeek();
if (endWeek < startWeek) {
// Yearly overlaps, month is either DEC or JANif (dateFirst.month() === 0) {
// Januaryreturn endWeek + 1;
} else {
// Decemberif (dateLast.isoWeekday() === 7) {
// Sunday is last day of yearreturn endWeek - startWeek + 1;
} else {
// Sunday is NOT last day of yearreturn dateFirst.isoWeeksInYear() - startWeek + 1;
}
}
} else {
return endWeek - startWeek + 1;
}
}
Outputs the following values for the following dates:
calcWeeksInMonth(moment("2016-12-01")); // 5calcWeeksInMonth(moment("2017-01-01")); // 6calcWeeksInMonth(moment("2017-02-01")); // 5calcWeeksInMonth(moment("2017-03-01")); // 5calcWeeksInMonth(moment("2017-04-01")); // 5calcWeeksInMonth(moment("2017-05-01")); // 5calcWeeksInMonth(moment("2017-06-01")); // 5calcWeeksInMonth(moment("2017-07-01")); // 6calcWeeksInMonth(moment("2017-08-01")); // 5calcWeeksInMonth(moment("2017-09-01")); // 5calcWeeksInMonth(moment("2017-10-01")); // 6calcWeeksInMonth(moment("2017-11-01")); // 5calcWeeksInMonth(moment("2017-12-01")); // 5calcWeeksInMonth(moment("2018-01-01")); // 5
OLD and very incorrect implementation:
calcWeeksInMonth(date) {
const dateFirst = moment(date).date(1)
const dateLast = moment(date).date(date.daysInMonth())
const startWeek = dateFirst.week()
const endWeek = dateLast.week()
if (endWeek < startWeek) {
return dateFirst.weeksInYear() - startWeek + 1 + endWeek
} else {
return endWeek - startWeek + 1
}
}
This seems to output correct results, feedback welcome if there is something I missed!
Solution 4:
function getWeekNums(momentObj) {
var clonedMoment = moment(momentObj), first, last;
// get week number for first day of month
first = clonedMoment.startOf('month').week();
// get week number forlast day of month
last = clonedMoment.endOf('month').week();
// In case last week is in next year
if( first > last) {
last = first + last;
}
returnlast - first + 1;
}
Solution 5:
It display the list of weeks in a month with 'moment.js'. It has been written in typescript with angular 6+.
Install moment with 'npm i moment'
Inside the ts file.
weeks_in_month() {
let year = 2019; // change yearlet month = 4; // change month herelet startDate = moment([year, month - 1])
let endDate = moment(startDate).endOf('month');
var dates = [];
var weeks = [];
var per_week = [];
var difference = endDate.diff(startDate, 'days');
per_week.push(startDate.toDate())
let index = 0;
let last_week = false;
while (startDate.add(1, 'days').diff(endDate) < 0) {
if (startDate.day() != 0) {
per_week.push(startDate.toDate())
}
else {
if ((startDate.clone().add(7, 'days').month() == (month - 1))) {
weeks.push(per_week)
per_week = []
per_week.push(startDate.toDate())
}
elseif (Math.abs(index - difference) > 0) {
if (!last_week) {
weeks.push(per_week);
per_week = [];
}
last_week = true;
per_week.push(startDate.toDate());
}
}
index += 1;
if ((last_week == true && Math.abs(index - difference) == 0) ||
(Math.abs(index - difference) == 0 && per_week.length == 1)) {
weeks.push(per_week)
}
dates.push(startDate.clone().toDate());
}
console.log(weeks);
}
Result:
Array of date moments.
[Array(6), Array(7), Array(7), Array(7), Array(3)]
0:(6) [MonApr012019 00:00:00 GMT+0530(IndiaStandardTime),
TueApr022019 00:00:00 GMT+0530(IndiaStandardTime),
WedApr032019 00:00:00 GMT+0530(IndiaStandardTime),
ThuApr042019 00:00:00 GMT+0530(IndiaStandardTime),
FriApr052019 00:00:00 GMT+0530(IndiaStandardTime),
SatApr062019 00:00:00 GMT+0530(IndiaStandardTime)]
1:(7) [SunApr072019 00:00:00 GMT+0530(IndiaStandardTime),
MonApr082019 00:00:00 GMT+0530(IndiaStandardTime),
TueApr092019 00:00:00 GMT+0530(IndiaStandardTime),
WedApr102019 00:00:00 GMT+0530(IndiaStandardTime),
ThuApr112019 00:00:00 GMT+0530(IndiaStandardTime),
FriApr122019 00:00:00 GMT+0530(IndiaStandardTime),
SatApr132019 00:00:00 GMT+0530(IndiaStandardTime)]
2:(7) [SunApr142019 00:00:00 GMT+0530(IndiaStandardTime),
MonApr152019 00:00:00 GMT+0530(IndiaStandardTime),
TueApr162019 00:00:00 GMT+0530(IndiaStandardTime),
WedApr172019 00:00:00 GMT+0530(IndiaStandardTime),
ThuApr182019 00:00:00 GMT+0530(IndiaStandardTime),
FriApr192019 00:00:00 GMT+0530(IndiaStandardTime),
SatApr202019 00:00:00 GMT+0530(IndiaStandardTime)]
3:(7) [SunApr212019 00:00:00 GMT+0530(IndiaStandardTime),
MonApr222019 00:00:00 GMT+0530(IndiaStandardTime),
TueApr232019 00:00:00 GMT+0530(IndiaStandardTime),
WedApr242019 00:00:00 GMT+0530(IndiaStandardTime),
ThuApr252019 00:00:00 GMT+0530(IndiaStandardTime),
FriApr262019 00:00:00 GMT+0530(IndiaStandardTime),
SatApr272019 00:00:00 GMT+0530(IndiaStandardTime)]
4:(3) [SunApr282019 00:00:00 GMT+0530(IndiaStandardTime),
MonApr292019 00:00:00 GMT+0530(IndiaStandardTime),
TueApr302019 00:00:00 GMT+0530(IndiaStandardTime)]
Post a Comment for "Moment Js Get Number Of Weeks In A Month"