<script>
function load()
{
var today = new Date();
var sDate = today.getYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate();
document.all.txtDate.value=sDate;
}
</script>
<body onload="load()">
<select name="sl" onchange="document.all.div1.style.display=''">
<option value=a>a
<option value=b>b
</select>
<p></p>
<div id="div1" style="display:none"><input type=text name='txtDate'></div>
</body>

解决方案 »

  1.   

    日期相减参考http://www.blogjava.net/mxx/archive/2006/09/27/72159.html。
      

  2.   

    我改了一下,樓主你試試:<script>
    function load()
    {
    var today = new Date();
    var sDate = today.getYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate();
    var beforeDate = today.getYear() + "-" + today.getMonth() + "-" + today.getDate();
    document.all.txtDate.value=sDate;
    document.all.txtBeforeDate.value=beforeDate;
    }
    </script>
    <body onload="load()">
    <select name="sl" onchange="document.all.div1.style.display=''">
    <option value=a>a
    <option value=b>b
    </select>
    <p></p>
    <div id="div1" style="display:none">
    <input type=text name='txtDate'><br>
    <input type=text name='txtBeforeDate'>
    </div>
    </body>
      

  3.   

    <script language="JavaScript">
    <!--
    String.prototype.parseDate = function(){
        var ar = (this + ",0,0,0").match(/\d+/g);
        return ar[5]?(new Date(ar[0],ar[1]-1,ar[2],ar[3],ar[4],ar[5])):(new Date());
    }/**//*
     * 功能:根据输入表达式返回日期字符串
     * 参数:dateFmt:字符串,由以下结构组成    
     *      yy:长写年,YY:短写年mm:数字月,MM:英文月,dd:日,hh:时,
     *      mi:分,ss秒,ms:毫秒,we:汉字星期,WE:英文星期.
     *      isFmtWithZero : 是否用0进行格式化,true or false
     * 返回:对应日期的中文字符串
    */
    Date.prototype.toString = function(dateFmt,isFmtWithZero){
        dateFmt = (dateFmt == null ? "yy-mm-dd hh:mi:ss" : dateFmt);
        if(typeof(dateFmt) != "string" )
            throw (new Error(-1, 'toString()方法的第一个参数为字符串类型!'));
        isFmtWithZero = !!isFmtWithZero;
        var weekArr=[["日","一","二","三","四","五","六"],["SUN","MON","TUR","WED","THU","FRI","SAT"]];
        var monthArr=["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
        var str=dateFmt;
        var o = {
            "yy" : this.getFullYear(),
            "YY" : this.getYear(),
            "mm" : this.getMonth()+1,
            "MM" : monthArr[this.getMonth()],
            "dd" : this.getDate(),
            "hh" : this.getHours(),
            "mi" : this.getMinutes(),
            "ss" : this.getSeconds(),
            "we" : "星期" + weekArr[0][this.getDay()],
            "WE" : weekArr[1][this.getDay()]
        }
        for(var i in o){
            str = str.replace(new RegExp(i,"g"),o[i].toString().fmtWithZero(isFmtWithZero));
        }
        str = str.replace(/ms/g,this.getMilliseconds().toString().fmtWithZeroD(isFmtWithZero));
        return str;
    }
    /**//*将一位数字格式化成两位,如: 9 to 09*/
    String.prototype.fmtWithZero = function(isFmtWithZero){    
        return (isFmtWithZero && /^\d$/.test(this))?"0"+this:this;
    }
    String.prototype.fmtWithZeroD = function(isFmtWithZero){    
        return (isFmtWithZero && /^\d{2}$/.test(this))?"00"+this:((isFmtWithZero && /^\d$/.test(this))?"0"+this:this);
    }/**//* 功能 : 返回与某日期相距N天(N个24小时)的日期
     * 参数 : num number类型 可以为正负整数或者浮点数,默认为1;
     *        type 0(秒) or 1(天),默认为天
     * 返回 : 新的Date对象
     */
    Date.prototype.dateAfter=function(num,type){
        num = (num == null?1:num);
        if(typeof(num)!="number") throw new Error(-1,"dateAfterDays(num,type)的num参数为数值类型.");
        type = (type==null?1:type);
        var arr = [1000,86400000];
        return new Date(this.valueOf() + num*arr[type]);
    }
    //-->
    </script>
    <script>
    function load()
    {
    var today = new Date();
    var sDate = today.getYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate();
    document.all.txtDate.value=sDate;var pDate = sDate.parseDate().dateAfter(-30,null);
    document.all.txtDate1.value=pDate.getYear() + "-" + (pDate.getMonth() + 1) + "-" + pDate.getDate();
    }
    </script>
    <body onload="load()">
    <select name="sl" onchange="document.all.div1.style.display=''">
    <option value=a>a
    <option value=b>b
    </select>
    <p></p>
    <div id="div1" style="display:none"><input type=text name='txtDate'><input type=text name='txtDate1'></div>
    </body>
      

  4.   

    <script>
    function load()
    {
    var txtDate = document.getElementById("txtDate");
    var txtDate_last = document.getElementById("txtDate_last");
    var today = new Date();
    var sDate = today.getYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate();
    var sDate_last = today.getYear() + "-" + (today.getMonth()) + "-" + today.getDate();
    txtDate.value=sDate;
    txtDate_last.value = sDate_last;}
    function f_show(){
    document.getElementById('div1').style.display='block';
    }
    </script>
    <body onload="load()">
    <select name="sl" onchange="f_show();">
    <option value=a>a
    <option value=b>b
    </select>
    <p></p>
    <div id="div1" style="display:none"><input type=text name='txtDate'>
    <input type=text name='txtDate_last'></div>
    </body>