0

我正在制作一个代码,当我选择开始日期时,结束日期将自动出现。结束日期是开始日期的 +3 天

<input type="date" name="start_date" id="start" onKeyUp="date()">
<input type="text" name="end_date" id = "end" onKeyUp="date()" disabled />

<script type = "text/javascript">
function date() {
var startdate = document.getElementById('start');
var enddate = document.getElementById('end');

enddate.value = startdate + 3 days; 
}
</script>

这个 enddate.value = startdate + 3 天;我知道代码是错误的。我不知道如何将日期传递给 javascript。请帮忙

4

3 回答 3

1
<input type="text" name="num_days" id = "days"  />
<input type="date" name="start_date" id="start" onChange="date()">
<input type="text" name="end_date" id = "end" onKeyUp="date()"  />

    <script type = "text/javascript">
    function date() {
         var numdays = document.getElementById('days');
    var startdate = document.getElementById('start');
    var enddate = document.getElementById('end');
    //conver you selected stuff to a proper date format
    var old = new Date(Date.parse(startdate.value));
    //take a new date
    var newdate = new Date();
        days = parseInt(numdays.value);
    // add how many days you want to add i use 3 you can use as many
    newdate.setDate(old.getDate()+days);
    enddate.value = newdate.getFullYear()+'-'+(newdate.getMonth()+1)+'-'+newdate.getDate(); 
    }
    </script>
于 2013-02-15T06:38:49.493 回答
0

只需通过开始日期date()

var today = new Date(document.getElementById('start'));
var dd = today.getDate();
var mm = today.getMonth()+1;//January is 0!
var yyyy = today.getFullYear();
if(dd<10){
    dd='0'+dd
}
if(mm<10){
    mm='0'+mm
}
var startdate=yyyy+'-'+mm+'-'+dd ;
var enddate=yyyy+'-'+mm+'-'+(parseInt(dd)+3) ;

这将为您提供 2 个日期:

  1. 开始日期

  2. 开始日期 + 3 天,即结束日期

我希望它会帮助你!

于 2013-02-15T06:38:58.190 回答
0

在这里试试DEMO

function mydate() {
     var targetDate = new Date();
     targetDate.setDate(targetDate.getDate() + 3); //how many days ahead

     var dd = targetDate.getDate();
     var mm = targetDate.getMonth() + 1; // 0 is January, so we must add 1
     var yyyy = targetDate.getFullYear();
     var dateString = yyyy + "-" + mm + "-" + dd;
     return dateString; 
}
于 2013-02-15T06:41:01.043 回答