0

我正在尝试用今天的日期预先填充一个文本框,但由于某种原因,它一直将今天的日期显示为 5/30/13,比一个月后。这段代码应该只是拉动当地时间,为什么会落后一个月呢?这是我的代码,有人可以告诉我为什么当前日期已关闭...请帮助,:)

<script type="text/javascript">
/*<![CDATA[*/
function my_curr_date() {      
var currentDate = new Date()
  var day = currentDate.getDate();
  var month = currentDate.getMonth() + 1;
  var year = currentDate.getFullYear();
var my_date = month+"-"+day+"-"+year;
document.getElementById("dateField").value=my_date;

}
function orderReady(orderTime){
dateToday.setDate(dateToday.getDate()+orderTime);
var ready=dateToday.getMonth()+"/"
+dateToday.getDate()+"/"+dateToday.getFullYear();
document.getElementById("duedateField").value=due_date;
}
/*]]>*/

<body onload='return my_curr_date();'>

<p>Today's Date<br />
<input type='text' name='dateField' id='dateField' value='' /><br />
Pick-up Date<br />
<input type='text' name='duedateField' id='duedateField' value='' /></p>
4

2 回答 2

2

日期.getMonth:

getMonth 返回的值是一个介于 0 和 11 之间的整数。0 对应于一月,1 到二月,依此类推。

参考

于 2013-06-30T22:17:49.740 回答
0

HTML 元素:

<p>Today's Date<br />
<input id="date" type="text" name="curDate" size="50" /><br />
Pick-up Date<br />
<input type='text' name='duedateField' id='duedateField' value='' /></p>

使用这个 javascript:

var month = Date.getMonth();
var date = Date.getDate();
var year = Date.getYear();
var deliverymonth;
var deliverydate;
var deliveryyear;    


var message = month + "/" + date + "/" + year;
document.getElementById('date').innerHTML=message;
if(month == 11 && date >= 29){
    deliveryyear = year + 1;
    deliverymonth = 1;
    deleverydate = date - 28;
}else{


     switch(month)
    {
        case 0,2,4,6,7,9,11:
             if(date >= 29){
                 deliverymonth = month + 1;
                 deliverydate = date - 28;
             }else{
                 deliverydate = date + 3;
             }
             break;
         case 3,5,8,10:
             if(date >= 28){
                 deliverymonth = month + 1;
                 deliverydate = date - 27;
             }else{
                 deliverydate = date + 3;
             }
             break;
          case 1:
             if(date >= 26){
                 deliverymonth = month + 1;
                 deliverydate = date - 25;
             }else{
                 deliverydate = date + 3;
             }
             break;
          default:
             break;
          }
var deliverymessage = deliverymonth + "/" + deliverydate + "/" + deliveryyear;
document.getElementById('duedateField').innerHTML=deliverymessage;
于 2013-06-30T22:38:59.010 回答