0

i'm making a code where when i selected a start date,an end date will automatically appear. the end date is +3 days of the start date

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

this enddate.value = startdate + 3 days; i know that code is wrong. i dont know how to pass dates to javascript. please help

1
  • I guess You need to create a date object Commented Feb 15, 2013 at 6:34

3 Answers 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>
Sign up to request clarification or add additional context in comments.

15 Comments

whats the problem you just want to pick a date and want to display 3 days ahead in right input box right?
i have edited my answer its working on my side.did you entered after picking up the date
still the same. i wonder what's the problem. i just copied the code that you gave to me.
i have added js fiddle pick a date when its done click on the same text box and enter on it
oh i see. so i need to enter first to get the value? hmm how to do it without pressing the enter and without the alert? i'm sorry i don't know much about javascript
|
0

try it here 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; 
}

5 Comments

but the dates are inputted by the user. how can i get the value of date box then pass it to js?
yes it's working. it adds the 3days in the current date. but what i want to happen is the user will input the date
well are you using a form?
why don't you post the form so we can better assist you?
<input type="date" name="start_date" id="start" onKeyUp="date()"> <input type="text" name="end_date" id = "end" onKeyUp="date()" disabled />
0

Just pass the start date in the 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) ;

This will give you 2 dates:

  1. Start date

  2. Start date + 3 days i.e enddate

I hope it will help you!

1 Comment

the dates are inputted by the user. i need to get the value inputted in the input date then pass it to js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.