文本字段中的预填充日期

时间:2013-06-30 22:12:15

标签: javascript html datepicker

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

<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>

2 个答案:

答案 0 :(得分:2)

Date.getMonth:

  

getMonth返回的值是0到11之间的整数   对应于1月1日至2月,依此类推。

Reference

答案 1 :(得分: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;