Javascript实现 前一天 今天 后一天
<script language ="javascript" type ="text/javascript">
function UpdateSrc()
{
if(document.getElementById ("txt_Time").value=="")
{
alert ("请选择时间");
return ;
}
document.getElementById("report").src = "AutoMPSSysList.aspx?Date=" + document.getElementById("txt_Time").value;
}
function changedate(val) {
var currdate = document.getElementById("txt_Time");
var date = new Date();
if (val == 0) {
currdate.value = date.getYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
}
else {
if (chkdate(currdate.value)) {
var dates = currdate.value.split("-");
dates[1] = dates[1].replace(/^0/g, '');
dates[2] = dates[2].replace(/^0/g, '');
var da = DayAddDiff(parseInt(dates[0]), parseInt(dates[1]), parseInt(dates[2]), val);
}
else {
var da = DayAddDiff(parseInt(date.getYear()), parseInt(date.getMonth() + 1), parseInt(date.getDate()), val);
}
currdate.value = da;
}
document.all.form1.ipSearch.onclick();
}
function DayAddDiff(year, month, day, diff)
{
var numDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var isLeap = false;
var newyear = year;
var newmonth = month - 1;
var n = numDays[newmonth];
var newday = day;
var newdiff = diff;
var ln;
if (newmonth == 0)
ln = 31;
else if(newmonth == 11)
ln = 31;
else
ln = numDays[newmonth + 1];
if (diff != 0){
//判断是否润年
if (year % 4 == 0) {
if (year % 100 != 0)
isLeap = true;
else {
if (year % 400 == 0)
isLeap = true;
}
}
if (newmonth == 1 && isLeap)
++n;
if (newmonth == 0 && isLeap)
++ln;
//加值
var newday = day + newdiff;
if (newday > 0) {
if (newday > n) {
newday = newday - n;
if (newmonth == 11)
{
newmonth = 0;
newyear += 1;
newdiff = newday - 1;
return DayAddDiff(newyear, newmonth + 1, 1, newdiff);
}
else {
newmonth += 1
newdiff = newday - 1;
return DayAddDiff(newyear, newmonth + 1, 1, newdiff);
}
}
}
else if (newday == 0) {
if (newmonth == 0)
{
newmonth = 11
newyear += -1;
newday = 31;
}
else {
newmonth += -1
newday = numDays[newmonth];
}
}
else {
if (newmonth == 0)
{
newmonth = 11
newyear += -1;
newdiff = newday;
newday = 31;
return DayAddDiff(newyear, newmonth + 1, newday, newdiff);
}
else {
newmonth += -1;
newdiff = newday;
newday = ln;
return DayAddDiff(newyear, newmonth + 1, newday, newdiff);
}
}
}
//输出字符串
var daystring = "";
daystring += year;
newmonth += 1;
//if (newmonth < 10)
// daystring += "-0" + newmonth;
//else
daystring += "-" + newmonth;
//if (newday < 10)
// daystring += "-0" + newday;
//else
daystring += "-" + newday;
return daystring;
}
function chkdate(datestr) {
var lthdatestr
if (datestr != "")
lthdatestr = datestr.length;
else
lthdatestr = 0;
var tmpy = "";
var tmpm = "";
var tmpd = "";
var datestr;
var status;
status = 0;
if (lthdatestr == 0)
//alert("日期不能为空!")
return false;
for (i = 0; i < lthdatestr; i++) {
if (datestr.charAt(i) == '-') {
status++;
}
if (status > 2) {
// alert("日期格式错误!");
return false;
}
if ((status == 0) && (datestr.charAt(i) != '-')) {
tmpy = tmpy + datestr.charAt(i)
}
if ((status == 1) && (datestr.charAt(i) != '-')) {
tmpm = tmpm + datestr.charAt(i)
}
if ((status == 2) && (datestr.charAt(i) != '-')) {
tmpd = tmpd + datestr.charAt(i)
}
}
year = new String(tmpy);
month = new String(tmpm);
day = new String(tmpd)
tempdate = new String(year + month + day);
//alert(tempdate);
if ((tmpy.length != 4) || (tmpm.length > 2) || (tmpd.length > 2)) {
//alert("日期格式错误!");
return false;
}
if (!((1 <= month) && (12 >= month) && (31 >= day) && (1 <= day))) {
//alert ("日期格式错误!");
return false;
}
if (!((year % 4) == 0) && (month == 2) && (day == 29)) {
//alert ("日期格式错误!");
return false;
}
if ((month <= 7) && ((month % 2) == 0) && (day >= 31)) {
//alert ("日期格式错误!");
return false;
}
if ((month >= 8) && ((month % 2) == 1) && (day >= 31)) {
//alert ("日期格式错误!");
return false;
}
if ((month == 2) && (day == 30)) {
//alert("日期格式错误!");
return false;
}
//alert("对啦");
return true;
}
</script>
选择时间: <asp:TextBox ID="txt_Time" runat ="server" ></asp:TextBox>
<input id="ipSearch" type="button" value="确定" class="btn_57" onclick="UpdateSrc()" />
<a href="javascript:" onclick="javascript:changedate(-1)">前一天</a>
<a href="javascript:" onclick="javascript:changedate(0)">今天</a>
<a href="javascript:" onclick="javascript:changedate(1)">后一天</a>