Moment js 具有获取一个月天数的功能:http: //momentjs.com/docs/#/displaying/days-in-month/
但是,我找不到查找一年中 iso 周数(52 或 53)的函数。
Moment js 具有获取一个月天数的功能:http: //momentjs.com/docs/#/displaying/days-in-month/
但是,我找不到查找一年中 iso 周数(52 或 53)的函数。
这是一个不依赖于库的答案。它使用一个函数来计算所需年份的 12 月 31 日所在的那一周。如果周为 1(即 12 月 31 日在下一年的第一周),则将天数向下移动,直到获得不同的值,这将是所需年份的最后一周。
function getWeekNumber(d) {
// Copy date so don't modify original
d = new Date(+d);
d.setHours(0, 0, 0, 0);
// Set to nearest Thursday: current date + 4 - current day number
// Make Sunday's day number 7
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
// Get first day of year
var yearStart = new Date(d.getFullYear(), 0, 1);
// Calculate full weeks to nearest Thursday
var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7)
// Return array of year and week number
return [d.getFullYear(), weekNo];
}
function weeksInYear(year) {
var month = 11,
day = 31,
week;
// Find week that 31 Dec is in. If is first week, reduce date until
// get previous week.
do {
d = new Date(year, month, day--);
week = getWeekNumber(d)[1];
} while (week == 1);
return week;
}
[2015, 2016, 2029, new Date().getFullYear()].forEach(year =>
console.log(`${year} has ${weeksInYear(year)} weeks`)
);
getWeekNumber代码来自这里: Get Week of year in JavaScript like in PHP。
或者,如果 12 月 31 日在下一年的第 1 周,则主题年有 52 周,否则有 53 周。
function getWeekNumber(d) {
d = new Date(+d);
d.setHours(0, 0, 0, 0);
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
var yearStart = new Date(d.getFullYear(), 0, 1);
var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7)
return [d.getFullYear(), weekNo];
}
function weeksInYear(year) {
var d = new Date(year, 11, 31);
var week = getWeekNumber(d)[1];
return week == 1 ? 52 : week;
}
[2015, 2016, 2029, new Date().getFullYear()].forEach(year =>
console.log(`${year} has ${weeksInYear(year)} weeks`)
);
在一年的最后一天使用 isoWeek 来获取周数,例如:
function weeksInYear(year) {
return Math.max(
moment(new Date(year, 11, 31)).isoWeek()
, moment(new Date(year, 11, 31-7)).isoWeek()
);
}
2014 年 2 月 4 日,将 weekInYear 和 isoWeeksInYear 函数添加到 moment.js
所以今天你可以使用moment().isoWeeksInYear()
或moment().weeksInYear()
有关更多信息,请参阅文档
获取一年中该周的所有周和期间,它可能对谁感兴趣
function getWeekPeriodsInYear(year) {
weeks = [];
// Get the first and last day of the year
currentDay = moment([year, 1]).startOf('year');
dayOfWeek = moment(currentDay).day();
lastDay = moment([year, 1]).endOf('year');
weeksInYear = moment(`${year}-01-01`).isoWeeksInYear();
daysToAdd = 7 - dayOfWeek;
for (let weekNumber = 1; weekNumber < weeksInYear + 1; weekNumber++) {
let endOfWeek = moment(currentDay).add(daysToAdd, 'days');
if (moment(endOfWeek).year() !== year) {
endOfWeek = lastDay;
}
weeks.push({ weekNumber, start: currentDay.toDate(), end: endOfWeek.toDate() });
currentDay = endOfWeek.add(1, 'day');
daysToAdd = 6;
}
return weeks;
}
getWeekPeriodsInYear(new Date().getFullYear()).forEach(period =>
document.write(`Week ${period.weekNumber} from ${moment(period.start).format('DD-MM-YYYY')} up to and including ${moment(period.end).format('DD-MM-YYYY')}<br/>`)
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>