0

Hello

I have two textfield and dropdown, i want add date to the first textfield and when change dropdown menu add year or month dynamically to the second text-field
and I used this code but it give
NaN/NaN/NaN

function calDate() {
var dateArr = document.getElementById('date_field1').value;
var date1 = new Date();
date1.setFullYear(dateArr[2],dateArr[1]-1,dateArr[0]);
date1.setDate(date1.getDate()+5);
document.getElementById("date_field2").value = date1.getDate() + "/" + (date1.getMonth()+1) + "/" + date1.getFullYear();
2
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by stbuchok
0

What are the values of dateArr[0], dateArr[1], dateArr[2]?

Also instead of having line 5, why not remove it and add 5 on line 4 (dateArr[0] + 5)

0

I want to add date at the text field and use dropdown menu when chnage the add month or add year and display it at the second textfield
i used another code

<script language="javascript">

function calDate() {
var d = new Date(document.getElementById('year1').value); 
document.getElementById("year1").value= d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate(); 
	if(document.form1.contact_status.value=='test'){

		
    d.setDate(d.getDate());
  
  document.getElementById("year2").value= d.getFullYear()+"-"+(d.getMonth()+2)+"-"+d.getDate(); 
	}
		else if	(document.form1.contact_status.value=='share'){
			
     d.setDate(d.getDate()+365);
    
 document.getElementById("year2").value= d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate(); 
	}
		
}
</script>

I want to add date in this format(y-m-d) when used give me "NaN-NaN-NaN"
how to make it accept ?

Edited by altarek: n/a

0

What is the value of document.getElementById('year1').value at line 4?
At line 5 what are the values of d.getFullYear(), d.getMonth() and d.getDate()?

Also what is the purpose of line 9, it seems to be doing nothing?

0

I just realized also, what happens if d.getMonth() returns 12? You are adding 2 to it which would make it 14 and not a valid number for a month.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.