/*默认显示上个月的日期*/
var nowdays = new Date();
var year = nowdays.getFullYear();
var month = nowdays.getMonth();
if(month==0){
month=12;
year=year-1;
}
if (month < 10) {
month = "0" + month;
}
var firstDay = year + "-" + month + "-" + "01";//上个月的第一天
month++;
var endDay = year + "-" + month + "-" + "01";//这个月的第一天
$('#start_time').val(firstDay);
$('#end_time').val(endDay);
/*获取上个月*/
function getPreMonth(date) {
var arr = date.split('-');
var year = arr[0]; //获取当前日期的年份
var month = arr[1]; //获取当前日期的月份
var day = arr[2]; //获取当前日期的日
var days = new Date(year, month, 0);
days = days.getDate(); //获取当前日期中月的天数
var year2 = year;
var month2 = parseInt(month) - 1;
if (month2 == 0) {
year2 = parseInt(year2) - 1;
month2 = 12;
}
var day2 = day;
var days2 = new Date(year2, month2, 0);
days2 = days2.getDate();
if (day2 > days2) {
day2 = days2;
}
if (month2 < 10) {
month2 = '0' + month2;
}
var t2 = year2 + '-' + month2 + '-' + day2;
return t2;
}
/*获取下个月*/
function getNextMonth(date) {
var arr = date.split('-');
var year = arr[0]; //获取当前日期的年份
var month = arr[1]; //获取当前日期的月份
var day = arr[2]; //获取当前日期的日
var days = new Date(year, month, 0);
days = days.getDate(); //获取当前日期中的月的天数
var year2 = year;
var month2 = parseInt(month) + 1;
if (month2 == 13) {
year2 = parseInt(year2) + 1;
month2 = 1;
}
var day2 = day;
var days2 = new Date(year2, month2, 0);
days2 = days2.getDate();
if (day2 > days2) {
day2 = days2;
}
if (month2 < 10) {
month2 = '0' + month2;
}
var t2 = year2 + '-' + month2 + '-' + day2;
return t2;
}
/*点击按钮*/
$("#pre_month").click(function(){
var start;
if($('#start_time').val()){
start = $('#start_time').val();
}else{
start = firstDay;
}
$('#start_time').datebox('setValue', getPreMonth(start));
$('#end_time').datebox('setValue', start);
});
$("#next_month").click(function(){
var start;
if($('#start_time').val()){
start = $('#start_time').val();
}else{
start = firstDay;
}
$('#start_time').datebox('setValue', getNextMonth(start));
$('#end_time').datebox('setValue', getNextMonth(getNextMonth(start)));
});
月份报表的时候需要