+ Start a Discussion

NaN (Not a Number) error while obtaining and appending date field value?

Hi All,


   I am developing code using Ajax in VisualForce. In the current working context, i am suppose to retrieve date from existing record(using java script query) and need to put it into another visualforce Date field. What i am doing is :I am taking day,month,year using getDay()...methods and appending the value to the new field like the code given below:


var date = new Date(endresult.CloseDate);

var day=date.getDate();

var month=date.getMonth()+1;

var year=date.getYear()+1900; document.getElementById("{!$Component.formid.pageblockid.pageblocksectionid1.opportunityclosedateid}").value=month+'/'+day+'/'+year;


In this context,. i am getting an NaN error. It is reporting that THE TEXT IN DATE FIELD IS NOT A NUMBER. How can i avoid this?

           And one more issue here is, this error is browser specific. In Chrome, i am not getting any error.. While, i am unable to execute the same code in  IE or Safari or Mozilla. 


 Can anyone tell me why this is happening and How to overcome this.?!?!?


  Tons of Thanks, 





Hi Nishanth,


     The problem here is that the date seperators are dashed when it is bieng retrieved from sf. While, the date constructor (In IE) only accepts the seperator in "/" (forward slash) format. So you need to transform the seperator dash to forward slash by doing the following


var dateString = new String();
dateString = endresult.CloseDate.split("T")[0];
/* normally date and time return together. If time is not present then ignore the split 
in previous line
var i=0; while(i<5){ if(dateString.match("-") != null){ if(dateString.match("-").length != 0){ dateString = dateString.replace("-","/"); }else{ break; } }else{ break; } i++; } var date = new Date(Date.parse(dateString));



Hope this helps.