js 前一天,后一天,上一月,下一月
直接上代码
//上一月
$("#bntUpMonth").click(function () {
var strstartdate = $("#tbBeginTime").val();
var curstartmonth = getPreMonth(strstartdate);
$("#tbBeginTime").val(curstartmonth.format("yyyy-MM-dd 00:00:00"));
var curendmonth = getNextMonth(curstartmonth);
$("#tbEndTime").val(curendmonth.format("yyyy-MM-dd 00:00:00"));
});
$("#bntNextMonth").click(function () {
var strstartdate = $("#tbBeginTime").val();
var curstartmonth = getNextMonth(strstartdate);
$("#tbBeginTime").val(curstartmonth.format("yyyy-MM-dd 00:00:00"))
var curendmonth = getNextMonth(curstartmonth);
$("#tbEndTime").val(curendmonth.format("yyyy-MM-dd 00:00:00"));
});
function getPreMonth(date) {
var curDate = new Date(date);
var strDate = curDate.format("yyyy-MM-dd");
var arr = strDate.split('-');
var year = arr[0]; //获取当前日期的年份
var month = arr[1]; //获取当前日期的月份
var day = 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;
}
month2 = month2 - 1;
if (month2 < 0) {
month2 = 12;
}
if (month2 < 10) {
month2 = '0' + month2;
}
var newDate = new Date(year2, month2, day2, 0, 0, 0, 0);
return newDate;
//var t2 = year2 + '-' + month2 + '-' + day2;
//return t2;
}
function getNextMonth(date) {
var curDate = new Date(date);
var strDate = curDate.format("yyyy-MM-dd");
var arr = strDate.split('-');
var year = arr[0]; //获取当前日期的年份
var month = arr[1]; //获取当前日期的月份
var day = 1; //获取当前日期的日
//var day = arr[2]; //获取当前日期的日
var days = new Date(year, month, 0);
days = days.getDate(); //获取当前日期中的月的天数
var year2 = year;
var month2 = parseInt(month);
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 newDate = new Date(year2, month2, day2, 0, 0, 0, 0);
return newDate;
//var t2 = year2 + '-' + month2 + '-' + day2;
//return t2;
}
Date.prototype.format = function (args) {
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"H+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"S": this.getMilliseconds() //millisecond
};
if (/(y+)/.test(args))
args = args.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o) if (new RegExp("(" + k + ")").test(args))
args = args.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
return args;
};
$("#bntUpDay").click(function () {
var strstartdate = $("#tbBeginTime").val();
var curstartDate = new Date(strstartdate);
curstartDate = new Date((curstartDate / 1000 - 86400) * 1000);
$("#tbBeginTime").val(curstartDate.format("yyyy-MM-dd HH:mm:ss"));
var strenddate = $("#tbEndTime").val();
var curendDate = new Date(strenddate);
curendDate = new Date((curendDate / 1000 - 86400) * 1000);
$("#tbEndTime").val(curendDate.format("yyyy-MM-dd HH:mm:ss"));
});
$("#bntNextDay").click(function () {
var strstartdate = $("#tbBeginTime").val();
var curstartDate = new Date(strstartdate);
curstartDate = new Date((curstartDate / 1000 + 86400) * 1000);
$("#tbBeginTime").val(curstartDate.format("yyyy-MM-dd HH:mm:ss"));
var strenddate = $("#tbEndTime").val();
var curendDate = new Date(strenddate);
curendDate = new Date((curendDate / 1000 + 86400) * 1000);
$("#tbEndTime").val(curendDate.format("yyyy-MM-dd HH:mm:ss"));
});