因为项目开发中遇到需要向后台传本周的开始和结束时间,以及上一周的起止时间,就琢磨了半天,总算写出来一套,写篇文章是为了方便自己记忆,也是分享给需要的人,水平有限,写的不好请见谅:
1、getDateStr3函数是为了把时间对象转变为yy-mm-dd的字符串,方便传值;
2、getWeekStartAndEnd函数是获取周的起止时间,并且用getDateStr3转换成字符串放到数组中,其中参数0代表当前周,-1代表前一周,-2代表上上周,以此类推,反过来也可以1代表下一周;
3、getMonthStartAndEnd函数是获取月的起止时间,传参同上
//获取当前日期yy-mm-dd //date 为时间对象 function getDateStr3(date) { var year = ""; var month = ""; var day = ""; var now = date; year = ""+now.getFullYear(); if((now.getMonth()+1)<10){ month = "0"+(now.getMonth()+1); }else{ month = ""+(now.getMonth()+1); } if((now.getDate())<10){ day = "0"+(now.getDate()); }else{ day = ""+(now.getDate()); } return year+"-"+month+"-"+day; } /** * 获得相对当前周AddWeekCount个周的起止日期 * AddWeekCount为0代表当前周 为-1代表上一个周 为1代表下一个周以此类推 * **/ function getWeekStartAndEnd(AddWeekCount) { //起止日期数组 var startStop = new Array(); //一天的毫秒数 var millisecond = 1000 * 60 * 60 * 24; //获取当前时间 var currentDate = new Date(); //相对于当前日期AddWeekCount个周的日期 currentDate = new Date(currentDate.getTime() + (millisecond * 7*AddWeekCount)); //返回date是一周中的某一天 var week = currentDate.getDay(); //返回date是一个月中的某一天 var month = currentDate.getDate(); //减去的天数 var minusDay = week != 0 ? week - 1 : 6; //获得当前周的第一天 var currentWeekFirstDay = new Date(currentDate.getTime() - (millisecond * minusDay)); //获得当前周的最后一天 var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6)); //添加至数组 startStop.push(getDateStr3(currentWeekFirstDay)); startStop.push(getDateStr3(currentWeekLastDay)); return startStop; } /** * 获得相对当月AddMonthCount个月的起止日期 * AddMonthCount为0 代表当月 为-1代表上一个月 为1代表下一个月 以此类推 * ***/ function getMonthStartAndEnd(AddMonthCount) { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = new Date(); var month=currentDate.getMonth()+AddMonthCount; if(month<0){ var n = parseInt((-month)/12); month += n*12; currentDate.setFullYear(currentDate.getFullYear()-n); } currentDate = new Date(currentDate.setMonth(month)); //获得当前月份0-11 var currentMonth = currentDate.getMonth(); //获得当前年份4位年 var currentYear = currentDate.getFullYear(); //获得上一个月的第一天 var currentMonthFirstDay = new Date(currentYear, currentMonth,1); //获得上一月的最后一天 var currentMonthLastDay = new Date(currentYear, currentMonth+1, 0); //添加至数组 startStop.push(getDateStr3(currentMonthFirstDay)); startStop.push(getDateStr3(currentMonthLastDay)); //返回 return startStop; }
获取到每月的第一天和最后一天 需要传入一个月份 如果忘记传入 取当前月份
//获取到每月的第一天和最后一天 getMonthFirstOrLaseDay:function(month){ var month=month || (new Date()).getMonth() //设置默认 如果不穿 取当前月份 var nowdays = new Date(); var year = nowdays.getFullYear(); if(month==0) { month=12; year=year-1; } if (month < 10) { month = "0" + month; } var firstDay = year+'' + month+'' + "01"; var myDate = new Date(year, month, 0); var lastDay = year+'' + month+'' + myDate.getDate(); return {firstDay:firstDay,lastDay:lastDay} },
获取到每个月有几周,并且每周一和周日是哪天 如果不穿 默认取当年 当月
//获取到每个月有几周,并且每周一和周日是哪天 getAForWeeks:function (year, month) { var year=year || (new Date()).getFullYear() var month=month || (new Date()).getMonth() var d = new Date(); // what day is first day d.setFullYear(year, month-1, 1); var w1 = d.getDay(); if (w1 == 0) w1 = 7; // total day of month d.setFullYear(year, month, 0); var dd = d.getDate(); // first Monday if (w1 != 1) d1 = 7 - w1 + 2; else d1 = 1; week_count = Math.ceil((dd-d1+1)/7); var allWeek={}; for (var i = 0; i < week_count; i++) { var monday = d1+i*7; var sunday = monday + 6; var from = year+''+this.fnToDub(month)+''+this.fnToDub(monday); var to; if (sunday <= dd) { to = year+''+this.fnToDub(month)+''+this.fnToDub(sunday); } else { d.setFullYear(year, month-1, sunday); to = d.getFullYear()+''+this.fnToDub((d.getMonth()+1))+''+this.fnToDub(d.getDate()); } allWeek[(i+1)]={ from:from, to:to } } return {allWeek:allWeek,week_count:week_count} },
获取当月的第一天和当月的最后一天其实还挺麻烦的,因为每个月天数可能不一样。不过借助 Date 对象则很容易实现:
构造函数
new Date();
new Date(value);
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
各参数的含义: