我正在使用一个返回当前一周的函数,如43,45等。
我想要的是将其作为本周的第1周第2周。
P.S我今年只有当前一周,没有约会
答案 0 :(得分:1)
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;
};
答案 1 :(得分:0)
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