<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<script type="text/javascript" src="js/jquery-1.8.2.js" ></script>
</head>
<body> <a id="test2" href="javascript: void(0);">前一天</a>
测试日期:<span id="test1">20200301</span>
<a id="test3" href="javascript: void(0);">后一天</a> <script type="text/javascript"> function getDateArr(year){
var yearFlag = false;
var januaryQty = 31;
var dateArr = [];
dateArr.push(januaryQty);
var februaryQty = 28;
if((year % 100 == 0
&& year % 400 == 0)
|| (year % 4 == 0
&& year % 100 != 0)){
februaryQty = 29;
}
dateArr.push(februaryQty);
var marchQty = 31;
var aprilQty = 30;
var mayQty = 31;
var juneQty = 30;
var julyQty = 31;
var augustQty = 31;
var septemberQty = 30;
var octoberQty = 31;
var novemberQty = 30;
var decemberQty = 31;
dateArr.push(marchQty);
dateArr.push(aprilQty);
dateArr.push(mayQty);
dateArr.push(juneQty);
dateArr.push(julyQty);
dateArr.push(augustQty);
dateArr.push(septemberQty);
dateArr.push(octoberQty);
dateArr.push(novemberQty);
dateArr.push(decemberQty);
return dateArr;
} $("#test2").on("click", function(){
var currentDateStr = $("#test1").text();
var currentYearStr = currentDateStr.substring(0, 4);
var dateArr = getDateArr(currentYearStr);
var currentMonthStr = currentDateStr.substring(4, 6);
var currentDayStr = currentDateStr.substring(6);
var currentMonthDayQty = dateArr[currentMonthStr - 1];
var priviousMonthDayIndex = currentMonthStr - 2;
if(priviousMonthDayIndex == -1){
priviousMonthDayIndex = dateArr.length - 1;
}
if(priviousMonthDayIndex == 12){
priviousMonthDayIndex = 0;
}
var priviousMonthDayQty = dateArr[priviousMonthDayIndex];
var previousYear = parseInt(currentYearStr);
var previousMonth = parseInt(currentMonthStr);
var previousDay = parseInt(currentDayStr) - 1;
var previousYearStr = previousYear + "";
var previousMonthStr = previousMonth + "";
var previousDayStr = previousDay + "";
if(previousDayStr < 1){
previousDayStr = priviousMonthDayQty;
previousMonthStr = parseInt(previousMonthStr) - 1;
}
if(previousMonthStr == 0){
previousYearStr = parseInt(previousYearStr) - 1;
previousMonthStr = 12;
}
if(previousMonthStr < 10){
previousMonthStr = "0" + previousMonthStr;
}
if(previousDayStr < 10){
previousDayStr = "0" + previousDayStr;
}
var previousDateStr = previousYearStr.toString() + previousMonthStr.toString() + previousDayStr.toString();
$("#test1").text(previousDateStr);
}); $("#test3").on("click", function(){
var currentDateStr = $("#test1").text();
var currentYearStr = currentDateStr.substring(0, 4);
var dateArr = getDateArr(currentYearStr);
var currentMonthStr = currentDateStr.substring(4, 6);
var currentDayStr = currentDateStr.substring(6);
var currentMonthDayQty = dateArr[currentMonthStr - 1];
var nextMonthDayIndex = currentMonthStr;
var nextYear = parseInt(currentYearStr);
var nextMonth = parseInt(currentMonthStr);
var nextDay = parseInt(currentDayStr) + 1;
if(nextDay > currentMonthDayQty){
nextDay = 1;
nextMonth = parseInt(nextMonth) + 1;
}
if(nextMonth == 13){
nextYear = parseInt(nextYear) + 1;
nextMonth = 1;
}
var nextYearStr = nextYear + "";
var nextMonthStr = nextMonth + "";
if(nextMonthStr < 10){
nextMonthStr = "0" + nextMonthStr;
}
var nextDayStr = nextDay + "";
if(nextDayStr < 10){
nextDayStr = "0" + nextDayStr;
}
var nextDateStr = nextYearStr.toString() + nextMonthStr.toString() + nextDayStr.toString();
$("#test1").text(nextDateStr);
}); </script> </body>
</html>
已完成.