简体   繁体   English

如何获取所有周的开始日期和结束日期

[英]How to get all the weeks start date and end date

How can we get all week start date and end date of current month?我们如何获得当月的所有周开始日期和结束日期?

Below code shows only current week start date and end date下面的代码仅显示当前周开始日期和结束日期

 var startOfWeek = moment().startOf("isoWeek").toDate(); var endOfWeek = moment().endOf("isoWeek").toDate(); console.log(startOfWeek) console.log(endOfWeek)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.0/moment.min.js" integrity="sha512-Izh34nqeeR7/nwthfeE0SI3c8uhFSnqxV0sI9TvTcXiFJkMd6fB644O64BRq2P/LA/+7eRvCw4GmLsXksyTHBg==" crossorigin="anonymous"></script>

Just add / subtract 7 days while you are in current month to get following and preceeding weeks.只需在当月添加/减去 7 天即可获得以下和前几周。

let current = startOfWeek;

do {
  current = current.subtract(7, "days");  //or .add(7, "days") to get next week
} while (current.month() === startOfWeek.month())

Is this interesting?这有趣吗? - no use of moment - 不使用片刻

No sure what you want to do with weeks that start before the 1st or end in the next month不确定您想从 1 号之前开始或在下个月结束的几周做什么

 const d = new Date(), month = d.getMonth(), yyyy = d.getFullYear(), end = new Date(yyyy,month+1,0).getDate(); const startEnd = Array.from(Array(end+1).keys()).slice(1) .reduce((acc,day) => { const d = new Date(yyyy,month,day,15,0,0,0); if (d.getDay() === 1) acc.push({start:d}); else if (d.getDay() === 6 && acc[acc.length-1]) acc[acc.length-1]["end"] = d; return acc },[]) console.log(startEnd)

The question mentions ISO weeks, so I guess you want any ISO week of the year that falls in the current month.这个问题提到了 ISO 周,所以我猜你想要一年中属于当月的任何 ISO 周。 You can do that by getting the 1st of the month, setting to the previous Monday (or same day if it is a Monday), then iterating over the weeks until you get to the next month.您可以通过获取本月的 1 日,设置为前一个星期一(如果是星期一,则设置为同一天),然后迭代数周直到下个月来实现。 Eg例如

 // Month is calendar month number function getISOWeeksInMonth(month = new Date().getMonth() + 1, year = new Date().getFullYear()) { // Start at 1st of month let weekStart = new Date(year, month - 1, 1); // Set to prior Monday weekStart.setDate(weekStart.getDate() - (weekStart.getDay() || 7) + 1); // Get end of week let weekEnd = new Date(weekStart); weekEnd.setDate(weekEnd.getDate() + 6); // Get week number of 1st week let weekNum = getWeekNumber(weekStart)[1]; // Create weeks array let weeks = []; while (weekStart.getMonth() < month) { weeks.push({ weekNum : weekNum++, start: new Date(weekStart), end: new Date(weekEnd) }); weekStart.setDate(weekStart.getDate() + 7); weekEnd.setDate(weekEnd.getDate() + 7); } return weeks; } // Get ISO week number - from https://stackoverflow.com/a/6117889/257182 function getWeekNumber(d) { d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate())); d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7)); var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1)); var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7); return [d.getUTCFullYear(), weekNo]; } // ISO weeks of current month getISOWeeksInMonth().forEach(week => console.log( 'Week : ' + week.weekNum + '\\nStart: ' + week.start.toDateString() + '\\nEnd : ' + week.end.toDateString()) ); // ISO weeks in Feb 2021 (starts on a Monday) getISOWeeksInMonth(2, 2021).forEach(week => console.log( 'Week : ' + week.weekNum + '\\nStart: ' + week.start.toDateString() + '\\nEnd : ' + week.end.toDateString()) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从javascript开始和结束日期开始几周 - Get weeks from start and end date in javascript 从 Angular 的开始和结束日期获取周数 - Get weeks from start and end date in Angular 使用js获取开始日期和结束日期之间的周数 - Get the number of weeks between a start date and end date using js 如何在开始日期的单独字段中添加天数/周数以在datepicker中获得结束日期? - how to add days / weeks from separate field in start date to get end date in datepicker? 如何使用时刻迭代周数,周的开始/结束日期 - How to iterate week number, start/end date of weeks using moment 通过选择从开始日期开始的周数或月数来自动结束日期 - Auto end date by selecting number of weeks or months from start date 使用角度计算开始日期和结束日期之间的周数 - calculate the number of weeks between start date and end date using angular 如何在React日期范围选择器中获取开始和结束日期值? - How to get start and end date value in React date range picker? 立即获取从 2022 年开始的一年中几周的所有开始和结束日期 - Get all the start and the end dates of weeks of a year starting 2022 in moment 如何获取开始日期和结束日期之间的日期? (周末除外) - How to get date between start date and end date? (excluding date on weekend)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM