/** * 获取昨天、今天、上周、本周、本季度、本年、本月、上月、上季度、上年的开始日期、结束日期 */ var now = new Date(); //当前日期 var hour = now.getHours();//得到小时 var minu = now.getMinutes();//得到分钟 var sec = now.getSeconds();//得到秒 var nowDayOfWeek = now.getDay(); //今天本周的第几天 var nowDay = now.getDate(); //当前日 var nowMonth = now.getMonth(); //当前月 var nowYear = now.getYear(); //当前年 nowYear += (nowYear < 2000) ? 1900 : 0; // var lastMonthDate = new Date(); //上月日期 lastMonthDate.setDate(1); lastMonthDate.setMonth(lastMonthDate.getMonth()-1); var lastYear = lastMonthDate.getYear(); var lastMonth = lastMonthDate.getMonth(); function addZero(i){ if (i<10) { i="0" + i; } return i; } //格式化日期:yyyy-MM-dd HHmmss function formatHHmmssDate(date) { var myyear = addZero(date.getFullYear()); var mymonth = addZero(date.getMonth()+1); var myweekday = addZero(date.getDate()); var myhour= addZero(date.getHours()); var myminu= addZero(date.getMinutes()); var mysec= addZero(date.getSeconds()); return (myyear+"-"+mymonth + "-" + myweekday+" "+myhour+":"+myminu+":"+mysec); } //格式化日期:yyyy-MM-dd function formatDate(date) { var myyear = addZero(date.getFullYear()); var mymonth = addZero(date.getMonth()+1); var myweekday = addZero(date.getDate()); return (myyear+"-"+mymonth + "-" + myweekday); } //格式化日期:yyyy-MM function formatYearMonthDate(date) { var myyear = addZero(date.getFullYear()); var mymonth = addZero(date.getMonth()+1); return (myyear+"-"+mymonth); } //获得某月的天数 function getMonthDays(myMonth){ var monthStartDate = new Date(nowYear, myMonth, 1); var monthEndDate = new Date(nowYear, myMonth + 1, 1); var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24); return days; } //获得本季度的开始月份 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; } //获得今天 function getTodayDate() { var now = new Date(); //当前日期 var hour = now.getHours();//得到小时 var minu = now.getMinutes();//得到分钟 var sec = now.getSeconds();//得到秒 //开始日期 var weekStartDate = new Date(nowYear, nowMonth, nowDay ); weekStartDate= formatDate(weekStartDate); $('#startTime').datebox('setValue', weekStartDate+" 00:00:00"); //结束日期 var weekEndDate = new Date(nowYear, nowMonth, nowDay,hour,minu,sec); weekEndDate= formatHHmmssDate(weekEndDate); $('#endTime').datebox('setValue',weekEndDate); } //获得昨天 function getYesterdayDate() { //开始日期 var weekStartDate = new Date(nowYear, nowMonth, nowDay -1); weekStartDate= formatDate(weekStartDate); $('#startTime').datebox('setValue', weekStartDate+" 00:00:00"); //结束日期 var weekEndDate = new Date(nowYear, nowMonth, nowDay -1); weekEndDate= formatDate(weekEndDate); $('#endTime').datebox('setValue',weekEndDate+" 23:59:59"); } //获得上周 function getLastWeekDate() { //开始日期 var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek-7); weekStartDate= formatDate(weekStartDate); $('#startTime').datebox('setValue', weekStartDate+" 00:00:00"); //结束日期 var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek-1); weekEndDate= formatDate(weekEndDate); $('#endTime').datebox('setValue',weekEndDate+" 23:59:59"); } //获得本周 function getWeekDate() { var now = new Date(); //当前日期 var hour = now.getHours();//得到小时 var minu = now.getMinutes();//得到分钟 var sec = now.getSeconds();//得到秒 //开始日期 var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek); console.log(weekStartDate) weekStartDate= formatDate(weekStartDate); $('#startTime').datebox('setValue', weekStartDate+" 00:00:00"); //结束日期 var weekEndDate = new Date(nowYear, nowMonth, nowDay,hour,minu,sec); weekEndDate= formatHHmmssDate(weekEndDate); $('#endTime').datebox('setValue',weekEndDate); } //获得本月 function getMonthDate(){ var now = new Date(); //当前日期 var hour = now.getHours();//得到小时 var minu = now.getMinutes();//得到分钟 var sec = now.getSeconds();//得到秒 //开始日期 var monthStartDate = new Date(nowYear, nowMonth, 1); monthStartDate= formatDate(monthStartDate); $('#startTime').datebox('setValue', monthStartDate); //结束日期 var weekEndDate = new Date(nowYear, nowMonth, nowDay,hour,minu,sec); weekEndDate= formatHHmmssDate(weekEndDate); $('#endTime').datebox('setValue',weekEndDate); } //获得上月 function getLastMonthDate(){ //开始时间 var lastMonthStartDate = new Date(nowYear, lastMonth, 1); monthStartDate= formatDate(lastMonthStartDate); $('#startTime').datebox('setValue', monthStartDate); //结束时间 var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth)); lastMonthEndDate= formatDate(lastMonthEndDate); $('#endTime').datebox('setValue',lastMonthEndDate); } //获得本季度 function getQuarterDate(){ var now = new Date(); //当前日期 var hour = now.getHours();//得到小时 var minu = now.getMinutes();//得到分钟 var sec = now.getSeconds();//得到秒 //开始日期 var quarterStartDate = new Date(nowYear, getQuarterStartMonth()); quarterStartDate= formatYearMonthDate(quarterStartDate); $('#startTime').datebox('setValue', quarterStartDate); //结束日期 var weekEndDate = new Date(nowYear, nowMonth, nowDay,hour,minu,sec); weekEndDate= formatHHmmssDate(weekEndDate); $('#endTime').datebox('setValue',weekEndDate); } //获得上季度 function getLastquarterDate(){ //开始日期 var quarterStartDate = new Date(nowYear, getQuarterStartMonth()-3); quarterStartDate= formatYearMonthDate(quarterStartDate); $('#startTime').datebox('setValue', quarterStartDate); //结束日期 var quarterEndMonth = getQuarterStartMonth()-1; var quarterStartDate = new Date(nowYear, quarterEndMonth); quarterStartDate= formatYearMonthDate(quarterStartDate); $('#endTime').datebox('setValue',quarterStartDate); } //获得本年 function getThisYearDate(){ var now = new Date(); //当前日期 var hour = now.getHours();//得到小时 var minu = now.getMinutes();//得到分钟 var sec = now.getSeconds();//得到秒 //结束日期 var weekEndDate = new Date(nowYear, nowMonth, nowDay,hour,minu,sec); weekEndDate= formatHHmmssDate(weekEndDate); $('#endTime').datebox('setValue',weekEndDate); //开始日期 $('#startTime').datebox('setValue',nowYear); } //获得上年 function getLastyearDate(){ $('#startTime').datebox('setValue',nowYear-1); $('#endTime').datebox('setValue',nowYear-1); }