I am having a function that is returning the current week of the year like 43,45 etc.
what I want is that to get it as week 1 week 2, for the current month .
P.S I only have current week of the year , Dont have date
I am having a function that is returning the current week of the year like 43,45 etc.
what I want is that to get it as week 1 week 2, for the current month .
P.S I only have current week of the year , Dont have date
var getWeekOfMonth = function(date) {
var month = date.getMonth(),
year = date.getFullYear(),
firstWeekday = new Date(year, month, 1).getDay(),
lastDateOfMonth = new Date(year, month + 1, 0).getDate(),
offsetDate = 7 - firstWeekday,
daysAfterFirstWeek = lastDateOfMonth - offsetDate,
weeksInMonth = Math.ceil(daysAfterFirstWeek / 7) + 1;
var noOfDaysAfterRemovingFirstWeek = date.getDate() - offsetDate;
if (noOfDaysAfterRemovingFirstWeek <= 0) {
week = 1;
} else if (noOfDaysAfterRemovingFirstWeek <= 7) {
week = 2;
} else if (noOfDaysAfterRemovingFirstWeek <= 14) {
week = 3;
} else if (noOfDaysAfterRemovingFirstWeek <= 21) {
week = 4;
} else if (weeksInMonth >= 5 && noOfDaysAfterRemovingFirstWeek <= 28) {
week = 5;
} else if (weeksInMonth === 6) {
week = 6;
}
return week;
};
Using 'moment' module:
var moment = require('moment');
function foo(weekOfYear) {
var dayOfMonth = moment().week(weekOfYear).date();
var weekOfMonth = Math.ceil(dayOfMonth / 7);
return weekOfMonth;
}
console.log(foo(1)) //1
console.log(foo(11)) //2
console.log(foo(12)) //3
console.log(foo(13)) //4
console.log(foo(14)) //1