js获取一周的日期范围
function getWeek() { this.nowTime = new Date(); this.init = function() { this.dayInWeek = this.nowTime.getDay(); this.dayInWeek == 0 && (this.dayInWeek = 7); this.thsiWeekFirstDay = this.nowTime.getTime() - (this.dayInWeek - 1) * 86400000; this.thisWeekLastDay = this.nowTime.getTime() + (7 - this.dayInWeek) * 86400000; return this; }; this.getWeekType = function(type) { type = ~~type; var firstDay = this.thsiWeekFirstDay + type * 7 * 86400000; var lastDay = this.thisWeekLastDay + type * 7 * 86400000; return this.getWeekHtml(firstDay, lastDay); } this.formateDate = function(time) { var newTime = new Date(time) var year = newTime.getFullYear(); var month = newTime.getMonth() + 1; var day = newTime.getDate(); return year + "-" + (month >= 10 ? month : "0" + month) + "-" + (day >= 10 ? day : "0" + day); }; this.getWeekHtml = function(f, l) { return this.formateDate(f) + "至" + this.formateDate(l); }; } var getWeek = new getWeek(); var week = getWeek.init().getWeekType(); console.log(week);
getWeekType()这个方法如果不传参数 或者传入0,返回的是本周的日期范围,如果要下周的范围则传入1,上周的传入-1;
注:这个是从周一开始算一周的开始,周日为结束。