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();
Member Avatar for stbuchok

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)

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 ?

Member Avatar for stbuchok

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?

Member Avatar for stbuchok

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.