点击切换前一天的日期 只能切换到当前月份的前三个月 例如(今天是2017年3月24日,点击左边按钮只能切换到2016年12月1日左按钮不能再点击,点击右边按钮只能切换到当前日期的前一天右按钮不能再点击)具体js代码如下:
var today = new Date(); 获取当前日期
var year = today.getFullYear();
var month = today.getMonth()+1;
var day = today.getDate();
var m = month;
var y =year;
var d =day;
var arrmonth = new Array([0],[31],[28],[31], [30], [31], [30], [31], [31], [30],[31], [30], [31]);
if (year % 4 == 0 && year % 100 != 0) { arrmonth[2] = 29; }
var value = year+"."+month+"."+day;
$('.time-num').html(value);
// 获取前三个月的日期
var beforeThreeMonth;
function getThreeMonth(m){
switch(m)
{
case 1:
case 2:
case 3:
beforeThreeMonth = m+10;
if(beforeThreeMonth==13){
beforeThreeMonth=1;
}
break;
default:
beforeThreeMonth = m-2;
break;
}
return beforeThreeMonth;
}
// 点击左边按钮时进行日期判断
function datel(){
if(day-1==0){
// 判断月份是否为一月
if(month-1==0){
year -= 1;
month = 12;
}else{
month -=1;
}
day = arrmonth[month];
}else{
day=day-1;
}
value = year+"."+month+"."+day;
$('.time-num').html(value);
}
// 点击右边按钮时进行日期判断
function dater(){
if(day+1>arrmonth[month]){
if(month+1==13){
year=year+1;
month=1;
}else{
month=month+1;
}
day = 1;
}else{
day=day+1;
}
value = year+"."+month+"."+day;
$('.time-num').html(value);
}
// 点击事件
$('.time-cont .left').on('click',function(){
getThreeMonth(m);
if(month == beforeThreeMonth&&day==1){
return false;
}else{
datel();
}
})
$('.time-cont .right').on('click',function(mon){
var mon = m;
if(month==mon&&d==day){
return false;
}else{
dater();
}
})
}