js 获取某年的某天是第几周
2015-03-04 15:17 醉月轩 阅读(16414) 评论(0) 编辑 收藏 举报1 /** 2 * 判断年份是否为润年 3 * 4 * @param {Number} year 5 */ 6 function isLeapYear(year) { 7 return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); 8 } 9 /** 10 * 获取某一年份的某一月份的天数 11 * 12 * @param {Number} year 13 * @param {Number} month 14 */ 15 function getMonthDays(year, month) { 16 return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28); 17 }26 /** 27 * 获取某年的某天是第几周 28 * @param {Number} y 29 * @param {Number} m 30 * @param {Number} d 31 * @returns {Number} 32 */ 33 function getWeekNumber(y, m, d) { 34 var now = new Date(y, m - 1, d), 35 year = now.getFullYear(), 36 month = now.getMonth(), 37 days = now.getDate(); 38 //那一天是那一年中的第多少天 39 for (var i = 0; i < month; i++) { 40 days += getMonthDays(year, i); 41 } 42 43 //那一年第一天是星期几 44 var yearFirstDay = new Date(year, 0, 1).getDay() || 7; 45 46 var week = null; 47 if (yearFirstDay == 1) { 48 week = Math.ceil(days / yearFirstDay); 49 } else { 50 days -= (7 - yearFirstDay + 1); 51 week = Math.ceil(days / 7) + 1; 52 } 53 54 return week; 55 }