javascript 获取本周和本月或任意月的时间段
最近有功能从两个时间段变成单个select选择了,但后端不想改接口。只好适配一下,把今天、本周、本月及过去的月份转换一下。
首先,更改一下select菜单,动态取出过去的月份,添加options。
var SearchTimer = ['全部', '今天', '本周', '本月'];
(function(){
var Month = new Date().getMonth();
for(; Month > 0; Month--){
SearchTimer.push(Month + '月份');
}
})();
接下来就是要把各个options在选择的时候转换成startTime和endTime.
switch(timer){
case '全部': break;
case '今天': break;
case '本周': break;
case '本月': break;
default:
}
然后是时间的处理,先需要格式化时间,注意补零,格式为yyyy-MM-dd HH:mm:ss。
function formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth()+1;
var myweekday = date.getDate();
var myhours = date.getHours();
var myminutes = date.getMinutes();
var myseconds = date.getSeconds();
if(mymonth < 10){mymonth = "0" + mymonth; }
if(myweekday < 10){myweekday = "0" + myweekday; }
if(myhours < 10){myhours = "0" + myhours; }
if(myminutes < 10){myminutes = "0" + myminutes; }
if(myseconds < 10){myseconds = "0" + myseconds; }
return (myyear+"-"+mymonth + "-" + myweekday+" "+myhours+":"+myminutes+":"+myseconds);
}
今天的时间很好获取,然后是本周时间:
var now = new Date(); //当前日期
var nowDayOfWeek = now.getDay(); //今天本周的第几天
var nowDay = now.getDate(); //当前日
var nowMonth = now.getMonth(); //当前月
var nowYear = now.getFullYear(); //当前年
function getWeekStartDate() { //获得本周的开端日期
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
return formatDate(weekStartDate); //调用上面的时间格式化
}
function getWeekEndDate() { //获得本周的停止日期
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
return formatDate(weekEndDate);
}
然后是本月时间,但首先需要知道本月有多少天:
function getMonthDays(myMonth){ //获得某月的天数
myMonth = parseInt(myMonth); //这一步很重要,有时候传进来的可能是字符串,后面+1的时候会变成字符串相连
var monthStartDate = new Date(nowYear, myMonth, 1); //当月1号的时间戳
var monthEndDate = new Date(nowYear, myMonth + 1, 1);//下月1号的时间戳
var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);//求差即可取得某月天数
return days;
}
function getMonthStartDate(){ //获得本月的开端日期
var monthStartDate = new Date(nowYear, nowMonth, 1);
return formatDate(monthStartDate);
}
function getMonthEndDate(){ //获得本月的停止日期
var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
return formatDate(monthEndDate);
}
然后是过去任意月份的1号和最后一天:
var theMon = timer.replace(/(月份)$/, '')-1; //先匹配出select的月份,如:‘9月份’,但getMonth()是从0天始的,所以需要减1,也正好可以转换成整型
startTime = formatDate(new Date(nowYear, theMon, 1));
endTime = formatDate(new Date(nowYear, theMon, getMonthDays(theMon)));//某月有几天,最后一天就是几天。
所以最后返回时间段:
var startTime, endTime;
endTime = formatDate(new Date());
switch(timer){
case '全部':
startTime = undefined;
endTime = undefined;
break;
case '今天':
startTime = formatDate(new Date(nowYear, nowMonth, nowDay));
break;
case '本周':
startTime = getWeekStartDate();
break;
case '本月':
startTime = getMonthStartDate();
break;
default:
var theMon = timer.replace(/(月份)$/, '')-1;
startTime = formatDate(new Date(nowYear, theMon, 1));
endTime = formatDate(new Date(nowYear, theMon, getMonthDays(theMon)));
break;
}
return {
startTime: startTime,
endTime: endTime
}
至此就转换完毕。
当然,如果有需要,还可能获取季度。
function getQuarterStartMonth(){ //获得本季度的开端月份
var quarterStartMonth = 0;
if(nowMonth<3){quarterStartMonth = 0; }//判断属于第一季度
if(2<nowMonth && nowMonth<6){quarterStartMonth = 3; }
if(5<nowMonth && nowMonth<9){quarterStartMonth = 6; }
if(nowMonth>8){quarterStartMonth = 9; }
return quarterStartMonth;
}
只要判断属于哪一季度,即可知道开端月份和结束月份。