Javascript實現 前一天 今天 後一天

來源:互聯網
上載者:User

<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()" />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <a href="javascript:" onclick="javascript:changedate(-1)">前一天</a>&nbsp;
                    <a href="javascript:" onclick="javascript:changedate(0)">今天</a>&nbsp;
                    <a href="javascript:" onclick="javascript:changedate(1)">後一天</a>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.