I need a round number of weeks per months as per the ISO standard.
Also I'm using momentJS rather than Js's Date.
according to ISO date formatting the week counts for that month if it has a thursday otherwise it counts for the previous month.
according to that the months who's rounded week count are 5 this year (2018) are :
Mars, May, August and November
the rest are said to have 4 weeks.
Here's my code (inspiration here):
EDIT if I don't apply the suggested fix in the comments, I end up with 1 week for the month of December for 2018, same for 2017.
ngOnInit() {
this.generateTimelineForGivenYear('2018');
}
generateTimelineForGivenYear(year){
for (let i = 1; i < 13; i++) {
let month;
if (i < 10) month = '0'.concat(i.toString());
else month = i;
this.howManyWeeksForGivenMonth(moment(year + '-'+ month + '-05'));
}
}
howManyWeeksForGivenMonth(myDate){
console.log('The Month : ', myDate.month() + 1);
const start = myDate.clone().startOf('month').week();
let end = myDate.clone().endOf('month').week();
if( start > end ){ end = 52 + end }
const res = end - start;
console.log('The Number of Weeks: ', res);
console.log('----------------------------------------------------');
}
my result :
The Month : 1
The Number of Weeks: 4
----------------------------------------------------
The Month : 2
The Number of Weeks: 4
----------------------------------------------------
The Month : 3
The Number of Weeks: 4
----------------------------------------------------
The Month : 4
The Number of Weeks: 4
----------------------------------------------------
The Month : 5
The Number of Weeks: 4
----------------------------------------------------
The Month : 6
The Number of Weeks: 4
----------------------------------------------------
The Month : 7
The Number of Weeks: 4
----------------------------------------------------
The Month : 8
The Number of Weeks: 4
----------------------------------------------------
The Month : 9
The Number of Weeks: 5
----------------------------------------------------
The Month : 10
The Number of Weeks: 4
----------------------------------------------------
The Month : 11
The Number of Weeks: 4
----------------------------------------------------
The Month : 12
The Number of Weeks: 5
----------------------------------------------------
as you can see I get two months that are 5 weeks and not the right ones either :
September and December
I tried this as well :
howManyWeeksForGivenMonth(myDate){
console.log('The Month : ', myDate.month() + 1);
const first = myDate.day() == 0 ? 6 : myDate.day()-1;;
const day = 7-first;
const last = myDate.daysInMonth();
let res = Math.floor((last-day)/7);
if ((last-day) % 7 !== 0) res++;
console.log('The Number of Weeks: ', res);
console.log('----------------------------------------------------');
}
which gave :
The Month : 1
The Number of Weeks: 4
----------------------------------------------------
The Month : 2
The Number of Weeks: 3
----------------------------------------------------
The Month : 3
The Number of Weeks: 4
----------------------------------------------------
The Month : 4
The Number of Weeks: 4
----------------------------------------------------
The Month : 5
The Number of Weeks: 5
----------------------------------------------------
The Month : 6
The Number of Weeks: 4
----------------------------------------------------
The Month : 7
The Number of Weeks: 4
----------------------------------------------------
The Month : 8
The Number of Weeks: 5
----------------------------------------------------
The Month : 9
The Number of Weeks: 4
----------------------------------------------------
The Month : 10
The Number of Weeks: 4
----------------------------------------------------
The Month : 11
The Number of Weeks: 4
----------------------------------------------------
The Month : 12
The Number of Weeks: 4
----------------------------------------------------
...not better.
what am I doing wrong?
UPDATE :
once figuring out me not cutting at Thursday was the issue, here's my new attempt :
howManyWeeksForGivenMonth(myDate){
console.log('The Month ', myDate.month() + 1 );
let weeks = 4
if(myDate.clone().startOf('month').weekday() >= 5 ){
weeks ++;
console.log('first week extra');
}
if(myDate.clone().endOf('month').weekday() < 5 ) {
weeks ++;
console.log('last week extra');
}
return weeks;
}
I get :
The Month 1
last week extra
5
The Month 2
last week extra
5
The Month 3
4
The Month 4
last week extra
5
The Month 5
last week extra
5
The Month 6
first week extra
5
The Month 7
last week extra
5
The Month 8
4
The Month 9
first week extra
last week extra
6
The Month 10
last week extra
5
The Month 11
4
The Month 12
first week extra
last week extra
6
which there's nothing to read into here it's just plain garbage.
Obviously I get that both ifs should never be triggered together for one month but I had supposed that would simply not happen.
I supposed wrong but besides that it still doesn't correspond to the right months. so what is it now?