获得一个月的一周

如何使用javascript / jquery获取周数?

例如:

第一周:2010年7月5日。/周数= 第一个星期一

上周:2010年7月12日。/周数= 第二个星期一

当前日期:2010年7月19日。/周数= 第三个星期一

下周:2010年7月26日。/周数= 上周一

function weekAndDay() { var date = new Date, days = ['Sunday','Monday','Tuesday','Wednesday', 'Thursday','Friday','Saturday'], prefixes = ['First', 'Second', 'Third', 'Fourth', 'Fifth']; return prefixes[0 | date.getDate() / 7] + ' ' + days[date.getDay()]; } weekAndDay(); // => "Third Monday" 

添加具有Last ...的function可能需要更多的黑客…

这是一个古老的问题,但存在的答案是完全错误的。 这是我的跨浏览器兼容解决方案:

  Date.prototype.getWeekOfMonth = function(exact) { var month = this.getMonth() , year = this.getFullYear() , firstWeekday = new Date(year, month, 1).getDay() , lastDateOfMonth = new Date(year, month + 1, 0).getDate() , offsetDate = this.getDate() + firstWeekday - 1 , index = 1 // start index at 0 or 1, your choice , weeksInMonth = index + Math.ceil((lastDateOfMonth + firstWeekday - 7) / 7) , week = index + Math.floor(offsetDate / 7) ; if (exact || week < 2 + index) return week; return week === weeksInMonth ? index + 5 : week; }; // Simple helper to parse YYYY-MM-DD as local function parseISOAsLocal(s){ var b = s.split(/\D/); return new Date(b[0],b[1]-1,b[2]); } // Tests console.log('Date Exact|expected not exact|expected'); [ ['2013-02-01', 1, 1],['2013-02-05', 2, 2],['2013-02-14', 3, 3], ['2013-02-23', 4, 4],['2013-02-24', 5, 6],['2013-02-28', 5, 6], ['2013-03-01', 1, 1],['2013-03-02', 1, 1],['2013-03-03', 2, 2], ['2013-03-15', 3, 3],['2013-03-17', 4, 4],['2013-03-23', 4, 4], ['2013-03-24', 5, 5],['2013-03-30', 5, 5],['2013-03-31', 6, 6] ].forEach(function(test){ var d = parseISOAsLocal(test[0]) console.log(test[0] + ' ' + d.getWeekOfMonth(true) + '|' + test[1] + ' ' + d.getWeekOfMonth() + '|' + test[2]); }); 
 week number = 0 | new Date().getDate() / 7 

这是几年,但我最近需要使用此function,并且对于2016/2020年的某些日期(例如1月31日),此处的代码都不起作用。

无论如何,这并不是最有效率的,但希望这可以帮助别人,因为这是我可以在这些年里和其他每一年一起工作的唯一事情。

 Date.prototype.getWeekOfMonth = function () { var dayOfMonth = this.getDay(); var month = this.getMonth(); var year = this.getFullYear(); var checkDate = new Date(year, month, this.getDate()); var checkDateTime = checkDate.getTime(); var currentWeek = 0; for (var i = 1; i < 32; i++) { var loopDate = new Date(year, month, i); if (loopDate.getDay() == dayOfMonth) { currentWeek++; } if (loopDate.getTime() == checkDateTime) { return currentWeek; } } }; 

在这个话题上也很挣扎 – 感谢Olson.dev! 如果有人被强迫,我已经缩短了他的function:

 // returns week of the month starting with 0 Date.prototype.getWeekOfMonth = function() { var firstWeekday = new Date(this.getFullYear(), this.getMonth(), 1).getDay(); var offsetDate = this.getDate() + firstWeekday - 1; return Math.floor(offsetDate / 7); } 

我觉得这很有效。 它返回从0开始的月中的一周:

 var d = new Date(); var date = d.getDate(); var day = d.getDay(); var weekOfMonth = Math.ceil((date - 1 - day) / 7); 

这本身就不支持。

你可以为这个推出自己的function,从这个月的第一天开始工作

 var currentDate = new Date(); var firstDayOfMonth = new Date( currentDate.getFullYear(), currentDate.getMonth(), 1 ); 

然后获得该日期的工作日:

 var firstWeekday = firstDayOfMonth.getDay(); 

…这将给你一个从零开始的索引,从0到6,其中0是星期日。

我只能找出一个更简单的代码来计算一年中给定月份的周数。

y == year例如{2012} m ==是{0 – 11}的值

 function weeks_Of_Month( y, m ) { var first = new Date(y, m,1).getDay(); var last = 32 - new Date(y, m, 32).getDate(); // logic to calculate number of weeks for the current month return Math.ceil( (first + last)/7 ); } 
 function weekNumberForDate(date){ var janOne = new Date(date.getFullYear(),0,1); var _date = new Date(date.getFullYear(),date.getMonth(),date.getDate()); var yearDay = ((_date - janOne + 1) / 86400000);//60 * 60 * 24 * 1000 var day = janOne.getUTCDay(); if (day<4){yearDay+=day;} var week = Math.ceil(yearDay/7); return week; } 

显然,一年中的第一周是包含该年第一个星期四的那一周。

在没有计算UTCDay的情况下,返回的一周比本来应该的情况低一周。 不自信这无法改善,但似乎现在有效。

我想你想使用weekOfMonth所以它会给1-4或1-5个月的月份。 我解决了同样的问题:

 var dated = new Date(); var weekOfMonth = (0 | dated.getDate() / 7)+1; 
 function getWeekOfMonth(date) { var nth = 0; // returning variable. var timestamp = date.getTime(); // get UTC timestamp of date. var month = date.getMonth(); // get current month. var m = month; // save temp value of month. while( m == month ) { // check if m equals our date's month. nth++; // increment our week count. // update m to reflect previous week (previous to last value of m). m = new Date(timestamp - nth * 604800000).getMonth(); } return nth; } 

在阅读完所有答案之后,我想出了一种比其他人使用更少CPU并且每年每个月的每一天工作的方法。 这是我的代码:

 function getWeekInMonth(year, month, day){ let weekNum = 1; // we start at week 1 let weekDay = new Date(year, month - 1, 1).getDay(); // we get the weekDay of day 1 weekDay = weekDay === 0 ? 6 : weekDay-1; // we recalculate the weekDay (Mon:0, Tue:1, Wed:2, Thu:3, Fri:4, Sat:5, Sun:6) let monday = 1+(7-weekDay); // we get the first monday of the month while(monday <= day) { //we calculate in wich week is our day weekNum++; monday += 7; } return weekNum; //we return it } 

我希望这可以提供帮助。

请尝试以下function。这个考虑周开始日期为星期一,星期结束日期为星期日。

 getWeekNumber(date) { var monthStartDate =new Date(new Date().getFullYear(), new Date().getMonth(), 1); monthStartDate = new Date(monthStartDate); var day = startdate.getDay(); date = new Date(date); var date = date.getDate(); return Math.ceil((date+ day-1)/ 7); } 
 function getWeekOfMonth(date) { const startWeekDayIndex = 1; // 1 MonthDay 0 Sundays const firstDate = new Date(date.getFullYear(), date.getMonth(), 1); const firstDay = firstDate.getDay(); let weekNumber = Math.ceil(`enter code here`(date.getDate() + firstDay) / 7); if (startWeekDayIndex === 1) { if (date.getDay() === 0 && date.getDate() > 1) { weekNumber -= 1; } if (firstDate.getDate() === 1 && firstDay === 0 && date.getDate() > 1) { weekNumber += 1; } } return weekNumber; } 

我希望这个测试工作到2025年