通过Javascript获得每月数周
在Javascript中,如何获得一个月的周数?我似乎在任何地方都找不到这方面的代码通过Javascript获得每月数周,javascript,calendar,Javascript,Calendar,在Javascript中,如何获得一个月的周数?我似乎在任何地方都找不到这方面的代码 /** * Returns count of weeks for year and month * * @param {Number} year - full year (2016) * @param {Number} month_number - month_number is in the range 1..12 * @returns {number} */ var weeksCount =
我需要这个来知道一个月需要多少行 更具体地说,我想要一周中至少有一天的周数(一周定义为从周日开始到周六结束) 所以,对于这样的事情,我想知道它有5周:/** * Returns count of weeks for year and month * * @param {Number} year - full year (2016) * @param {Number} month_number - month_number is in the range 1..12 * @returns {number} */ var weeksCount = function(year, month_number) { var firstOfMonth = new Date(year, month_number - 1, 1); var day = firstOfMonth.getDay() || 6; day = day === 1 ? 0 : day; if (day) { day-- } var diff = 7 - day; var lastOfMonth = new Date(year, month_number, 0); var lastDate = lastOfMonth.getDate(); if (lastOfMonth.getDay() === 1) { diff--; } var result = Math.ceil((lastDate - diff) / 7); return result + 1; };
S M T W R F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
谢谢你的帮助。你得计算一下 你可以这样做现在我们只需要将其泛化var firstDay = new Date(2010, 0, 1).getDay(); // get the weekday january starts on var numWeeks = 5 + (firstDay >= 5 ? 1 : 0); // if the months starts on friday, then it will end on sunday
注意:在调用时,请记住在javascript中月份是零索引的(即一月==0)。您可以使用。以下是weeksInMonth函数:var dayThreshold = [ 5, 1, 5, 6, 5, 6, 5, 5, 6, 5, 6, 5 ]; function GetNumWeeks(month, year) { var firstDay = new Date(year, month, 1).getDay(); var baseWeeks = (month == 1 ? 4 : 5); // only February can fit in 4 weeks // TODO: account for leap years return baseWeeks + (firstDay >= dayThreshold[month] ? 1 : 0); // add an extra week if the month starts beyond the threshold day. }
这可能有点模糊,因为该功能的核心是endOfMonth和firstDayInCalendarMonth,但您至少应该能够了解它是如何工作的// http://github.com/augustl/time.js/blob/623e44e7a64fdaa3c908debdefaac1618a1ccde4/time.js#L67 weeksInMonth: function(){ var millisecondsInThisMonth = this.clone().endOfMonth().epoch() - this.clone().firstDayInCalendarMonth().epoch(); return Math.ceil(millisecondsInThisMonth / MILLISECONDS_IN_WEEK); },
function weeksinMonth(m, y){ y= y || new Date().getFullYear(); var d= new Date(y, m, 0); return Math.floor((d.getDate()- 1)/7)+ 1; } alert(weeksinMonth(3))
//此方法的月份范围为1(1月)-12(12月)从周日开始的几周function weekCount(year, month_number) { // month_number is in the range 1..12 var firstOfMonth = new Date(year, month_number-1, 1); var lastOfMonth = new Date(year, month_number, 0); var used = firstOfMonth.getDay() + 6 + lastOfMonth.getDate(); return Math.ceil( used / 7); }
即使二月不是星期天开始,这也应该有效function weekCount(year, month_number, startDayOfWeek) { // month_number is in the range 1..12 // Get the first day of week week day (0: Sunday, 1: Monday, ...) var firstDayOfWeek = startDayOfWeek || 0; var firstOfMonth = new Date(year, month_number-1, 1); var lastOfMonth = new Date(year, month_number, 0); var numberOfDaysInMonth = lastOfMonth.getDate(); var firstWeekDay = (firstOfMonth.getDay() - firstDayOfWeek + 7) % 7; var used = firstWeekDay + numberOfDaysInMonth; return Math.ceil( used / 7); }
周从周一开始function weekCount(year, month_number) { // month_number is in the range 1..12 var firstOfMonth = new Date(year, month_number-1, 1); var lastOfMonth = new Date(year, month_number, 0); var used = firstOfMonth.getDay() + lastOfMonth.getDate(); return Math.ceil( used / 7); }
function weekCount(year, month_number) { // month_number is in the range 1..12 var firstOfMonth = new Date(year, month_number-1, 1); var lastOfMonth = new Date(year, month_number, 0); var used = firstOfMonth.getDay() + 6 + lastOfMonth.getDate(); return Math.ceil( used / 7); }
几周后改天开始function weekCount(year, month_number, startDayOfWeek) { // month_number is in the range 1..12 // Get the first day of week week day (0: Sunday, 1: Monday, ...) var firstDayOfWeek = startDayOfWeek || 0; var firstOfMonth = new Date(year, month_number-1, 1); var lastOfMonth = new Date(year, month_number, 0); var numberOfDaysInMonth = lastOfMonth.getDate(); var firstWeekDay = (firstOfMonth.getDay() - firstDayOfWeek + 7) % 7; var used = firstWeekDay + numberOfDaysInMonth; return Math.ceil( used / 7); }
function weekCount(year, month_number) { // month_number is in the range 1..12 var firstOfMonth = new Date(year, month_number-1, 1); var lastOfMonth = new Date(year, month_number, 0); var used = firstOfMonth.getDay() + 6 + lastOfMonth.getDate(); return Math.ceil( used / 7); }
function weekCount(year, month_number, startDayOfWeek) { // month_number is in the range 1..12 // Get the first day of week week day (0: Sunday, 1: Monday, ...) var firstDayOfWeek = startDayOfWeek || 0; var firstOfMonth = new Date(year, month_number-1, 1); var lastOfMonth = new Date(year, month_number, 0); var numberOfDaysInMonth = lastOfMonth.getDate(); var firstWeekDay = (firstOfMonth.getDay() - firstDayOfWeek + 7) % 7; var used = firstWeekDay + numberOfDaysInMonth; return Math.ceil( used / 7); }
感谢Ed Poor的解决方案,这与Date原型相同所以你可以像这样使用它Date.prototype.countWeeksOfMonth = function() { var year = this.getFullYear(); var month_number = this.getMonth(); var firstOfMonth = new Date(year, month_number-1, 1); var lastOfMonth = new Date(year, month_number, 0); var used = firstOfMonth.getDay() + lastOfMonth.getDate(); return Math.ceil( used / 7); }
var weeksInCurrentMonth = new Date().countWeeksOfMonth(); var weeksInDecember2012 = new Date(2012,12,1).countWeeksOfMonth(); // 6
这是非常简单的两行代码。我已经测试了100%如何使用Date.prototype.getWeekOfMonth = function () { var firstDay = new Date(this.setDate(1)).getDay(); var totalDays = new Date(this.getFullYear(), this.getMonth() + 1, 0).getDate(); return Math.ceil((firstDay + totalDays) / 7); }
var totalWeeks = new Date().getWeekOfMonth(); console.log('Total Weeks in the Month are : + totalWeeks );
这段代码给出了给定月份的确切周数:Date.prototype.getMonthWeek = function(monthAdjustement) { var firstDay = new Date(this.getFullYear(), this.getMonth(), 1).getDay(); var returnMessage = (Math.ceil(this.getDate()/7) + Math.floor(((7-firstDay)/7))); return returnMessage; }
变量添加或减去您当前所在的月份monthadjustment
我在JS和Objective-C中的日历项目中使用了它,它工作得很好最容易理解的方法是<div id="demo"></div> <script type="text/javascript"> function numberOfDays(year, month) { var d = new Date(year, month, 0); return d.getDate(); } function getMonthWeeks(year, month_number) { var $num_of_days = numberOfDays(year, month_number) , $num_of_weeks = 0 , $start_day_of_week = 0; for(i=1; i<=$num_of_days; i++) { var $day_of_week = new Date(year, month_number, i).getDay(); if($day_of_week==$start_day_of_week) { $num_of_weeks++; } } return $num_of_weeks; } var d = new Date() , m = d.getMonth() , y = d.getFullYear(); document.getElementById('demo').innerHTML = getMonthWeeks(y, m); </script>
功能天数(年、月) { var d=新日期(年、月、0); 返回d.getDate(); } 函数getMonthWeeks(年、月和编号) { var$num_of_days=numberOfDays(年、月编号) ,$num_/u周=0 ,$start\u day\u of_week=0; 对于(i=1;i 这对我有用,请试试 感谢大家使用moment js
function getWeeksInMonth(year, month){ var monthStart = moment().year(year).month(month).date(1); var monthEnd = moment().year(year).month(month).endOf('month'); var numDaysInMonth = moment().year(year).month(month).endOf('month').date(); //calculate weeks in given month var weeks = Math.ceil((numDaysInMonth + monthStart.day()) / 7); var weekRange = []; var weekStart = moment().year(year).month(month).date(1); var i=0; while(i<weeks){ var weekEnd = moment(weekStart); if(weekEnd.endOf('week').date() <= numDaysInMonth && weekEnd.month() == month) { weekEnd = weekEnd.endOf('week').format('LL'); }else{ weekEnd = moment(monthEnd); weekEnd = weekEnd.format('LL') } weekRange.push({ 'weekStart': weekStart.format('LL'), 'weekEnd': weekEnd }); weekStart = weekStart.weekday(7); i++; } return weekRange; } console.log(getWeeksInMonth(2016, 7))
函数getWeeksInMonth(年,月){ var monthStart=moment().year(year)、month(month)、date(1); var monthEnd=矩().year(year).month(month.endOf('month'); var numDaysInMonth=moment().year(year).month(month).endOf('month').date(); //计算给定月份的周数 var weeks=Math.ceil((numDaysInMonth+monthStart.day())/7); var weekRange=[]; var weekStart=moment().year(year)、month(month)、date(1); var i=0;
虽然(i这里提出的任何解决方案都不能正确工作,所以我编写了自己的变体,它适用于任何情况。 简单有效的解决方案: 这对我来说很有用/** * Returns number of weeks * * @param {Number} year - full year (2018) * @param {Number} month - zero-based month index (0-11) * @param {Boolean} fromMonday - false if weeks start from Sunday, true - from Monday. * @returns {number} */ const weeksInMonth = (year, month, fromMonday = false) => { const first = new Date(year, month, 1); const last = new Date(year, month + 1, 0); let dayOfWeek = first.getDay(); if (fromMonday && dayOfWeek === 0) dayOfWeek = 7; let days = dayOfWeek + last.getDate(); if (fromMonday) days -= 1; return Math.ceil(days / 7); }
“d”应该是日期。ES6变量,使用一致的零基月指数。从2015年到2025年进行测试function convertDate(date) {//i lost the guy who owns this code lol var yyyy = date.getFullYear().toString(); var mm = (date.getMonth()+1).toString(); var dd = date.getDate().toString(); var mmChars = mm.split(''); var ddChars = dd.split(''); return yyyy + '-' + (mmChars[1]?mm:"0"+mmChars[0]) + '-' + (ddChars[1]?dd:"0"+ddChars[0]); } //this line of code from https://stackoverflow.com/a/4028614/2540911 var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; var myDate = new Date('2019-03-2'); //var myDate = new Date(); //or todays date var c = convertDate(myDate).split("-"); let yr = c[0], mth = c[1], dy = c[2]; weekCount(yr, mth, dy) //Ahh yes, this line of code is from Natim Up there, incredible work, https://stackoverflow.com/a/2485172/2540911 function weekCount(year, month_number, startDayOfWeek) { // month_number is in the range 1..12 console.log(weekNumber); // Get the first day of week week day (0: Sunday, 1: Monday, ...) var firstDayOfWeek = startDayOfWeek || 0; var firstOfMonth = new Date(year, month_number-1, 1); var lastOfMonth = new Date(year, month_number, 0); var numberOfDaysInMonth = lastOfMonth.getDate(); var first = firstOfMonth.getDate(); //initialize first week let weekNumber = 1; while(first-1 < numberOfDaysInMonth){ // add a day firstOfMonth = firstOfMonth.setDate(firstOfMonth.getDate() + 1);//this line of code from https://stackoverflow.com/a/9989458/2540911 if(days[firstOfMonth.getDay()] === "Sunday"){//get new week every new sunday according the local date format //get newWeek weekNumber++; } if(weekNumber === 3 && days[firstOfMonth.getDay()] === "Friday") alert(firstOfMonth); first++ } }
我知道这已经很晚了,我见过一个又一个的代码试图得到一个特定月份的周数,但是很多代码都不是很精确,但大多数都是非常有用的和可重用的,我不是一个专业的程序员,但我真的可以思考,多亏了一些人的一些代码,我才能够得出结论// Example // weeksOfMonth(2019, 9) // October // Result: 5 weeksOfMonth (year, monthIndex) { const d = new Date(year, monthIndex+ 1, 0) const adjustedDate = d.getDate() + d.getDay() return Math.ceil(adjustedDate / 7) }
function convertDate(date){//我失去了拥有这段代码的人,哈哈 var yyyy=date.getFullYear().toString(); var mm=(date.getMonth()+1.toString(); var dd=date.getDate().toString(); 变量mmChars=mm.分割(“”); var ddChars=dd.split(“”); 返回yyyy+'-'+(mmChars[1]?mm:“0”+mmChars[0])+'-'+(ddChars[1]?dd:“0”+ddChars[0]); } //这行代码来自https://stackoverflow.com/a/4028614/2540911 var days=[‘星期日’、‘星期一’、‘星期二’、‘星期三’、‘星期四’、‘星期五’、‘星期六’]; var myDate=新日期('2019-03-2'); //var myDate=新日期();//或今天日期 var c=转换日期(myDate)。拆分(“-”); 设yr=c[0],mth=c[1],dy=c[2]; 周数(年、月、日) //啊,是的,这行代码来自Natim,难以置信的工作,https://stackoverflow.com/a/2485172/2540911 功能周数(年、月、开始日期周){ //月号在1到12之间 控制台日志(周号); //获取一周中的第一天(0:星期日,1:星期一,…) var firstDayOfWeek=startDayOfWeek | | 0; var firstOfMonth=新日期(年、月、编号1、1); var lastOfMonth=新日期(年、月\号,0); var numberOfDaysInMonth=lastOfMonth.getDate(); var first=firstOfMonth.getDate(); //初始化第一周 让weekNumber=1; 而(第一个-1
我需要这段代码在每个月的第三个星期五为教堂生成一个时间表或事件计划程序,所以你可以修改它以适应你的需要,或者只选择你的具体日期,而不是“星期五并指定一个月的星期,瞧!!这里没有一个解决方案真正适合我。这是我的破解方法/** * @param {date} 2020-01-30 * @return {int} count */ this.numberOfCalendarWeekLines = date => { // get total let lastDayOfMonth = new Date( new Date( date ).getFullYear(), new Date( date ).getMonth() + 1, 0 ); let manyDaysInMonth = lastDayOfMonth.getDate(); // itterate through month - from 1st // count calender week lines by occurance // of a Saturday ( s m t w t f s ) let countCalendarWeekLines = 0; for ( let i = 1; i <= manyDaysInMonth; i++ ) { if ( new Date( new Date( date ).setDate( i ) ).getDay() === 6 ) countCalendarWeekLines++; } // days after last occurance of Saturday // leaked onto new line? if ( lastDayOfMonth.getDay() < 6 ) countCalendarWeekLines++; return countCalendarWeekLines; };
有点简陋,但应符合原始帖子:/** *@param{date}2020-01-30 *@return{int}计数 */ this.numberOfCalendarWeekLines=日期=>{ //合计 设lastDayOfMonth=新日期(新日期.getFullYear(),新日期.getMonth()+1,0); 设manyDaysInMonth=lastDayOfMonth.getDate(); //从第1个月到第1个月 //按发生次数计算日历周行数 //星期六的星期六(s m t w t f s) 设countCalendarWeekLines=0;
对于(让i=1;i),您的问题缺少一些参数。请更具体一些。您需要数字吗