js 获取年、月、周、当前日期第几周、这月有那几周
查看当前日期是第几周:https://wannianli.tianqi.com/today/zhou/
//获取完整的日期 var date=new Date; var y = date.getFullYear() var m = date.getMonth() + 1 var temptime=""; temptime= new Date(y - 2, m, d) // 往前一年 temptime= new Date(y , m+4-1, d) //往后4个月
获取当前第几周 function theWeek() { var totalDays = 0; now = new Date(); years = now.getYear() if (years < 1000) years += 1900 var days = new Array(12); days[0] = 31; days[2] = 31; days[3] = 30; days[4] = 31; days[5] = 30; days[6] = 31; days[7] = 31; days[8] = 30; days[9] = 31; days[10] = 30; days[11] = 31; //判断是否为闰年,针对2月的天数进行计算 if (Math.round(now.getYear() / 4) == now.getYear() / 4) { days[1] = 29 } else { days[1] = 28 } if (now.getMonth() == 0) { totalDays = totalDays + now.getDate(); } else { var curMonth = now.getMonth(); for (var count = 1; count <= curMonth; count++) { totalDays = totalDays + days[count - 1]; } totalDays = totalDays + now.getDate(); } //得到第几周 var week = Math.round(totalDays / 7); return week; }
获取当月有那几周 function GetCustomWeekList(year, month) { var rtn_Weeks = []; // 获取当月属于当年的第几周 function getWeek(dt) { let d1 = new Date(dt); let d2 = new Date(dt); d2.setMonth(0); d2.setDate(1); let rq = d1 - d2; let days = Math.ceil(rq / (24 * 60 * 60 * 1000)); let num = Math.ceil(days / 7); return num; } //获取当前月有几周 function getMonthWeek(a, b, c) { var date = new Date(a, parseInt(b) - 1, c), w = date.getDay(), d = date.getDate(); return Math.ceil( (d + 6 - w) / 7 ); }; var last = new Date(year, month, 0);//获取当前月最后一天时间 var y = last.getYear(); var m = last.getMonth() + 1; var d = last.getDate(); var ss = getWeek(year + "-" + month + "-1") for (var i = 0; i < getMonthWeek(y, m, d); i++) { item = []; item.push(ss + i); rtn_Weeks.push(item); } return rtn_Weeks; }