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
asked Feb 15, 2013 at 6:30
arukiri123
1411 gold badge2 silver badges16 bronze badges
-
I guess You need to create a date objectScoRpion– ScoRpion2013年02月15日 06:34:15 +00:00Commented Feb 15, 2013 at 6:34
3 Answers 3
<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>
answered Feb 15, 2013 at 6:38
rohitarora
1,3752 gold badges13 silver badges22 bronze badges
Sign up to request clarification or add additional context in comments.
15 Comments
rohitarora
whats the problem you just want to pick a date and want to display 3 days ahead in right input box right?
rohitarora
i have edited my answer its working on my side.did you entered after picking up the date
arukiri123
still the same. i wonder what's the problem. i just copied the code that you gave to me.
rohitarora
i have added js fiddle pick a date when its done click on the same text box and enter on it
arukiri123
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
|
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
arukiri123
but the dates are inputted by the user. how can i get the value of date box then pass it to js?
arukiri123
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
Keezy
well are you using a form?
Keezy
why don't you post the form so we can better assist you?
arukiri123
<input type="date" name="start_date" id="start" onKeyUp="date()"> <input type="text" name="end_date" id = "end" onKeyUp="date()" disabled />
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:
Start date
Start date + 3 days i.e enddate
I hope it will help you!
1 Comment
arukiri123
the dates are inputted by the user. i need to get the value inputted in the input date then pass it to js
default