function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
csathishbabucsathishbabu 

Difference between dates in javascript page

Im using a custom button in opportunity tab to get INvoice for a particular Opportunity,In that case the Invoice PAge should Invoke only wen the Opportunity Created date should be less than 21 days from now,so if difference between is greater 21 days it should not invoke Here is my COde in Custom button link display type is detail page button if({!Opportunity.CloseDate} - {!Opportunity.CreatedDate} > 21){ window.parent.location.href = "/apex/Invoice_1?id="+'{!Opportunity.Id}'; }else{ //do nothing } Thanks in Advance Sathish
SargeSarge

Hi Satish,

 

   First it is required to calculate number of days in javascript. Following code can aid you in finding difference in days

 

 

var createdDate = new Date('{!Opportunity.CreatedDate}'); 
  //get opportunity created dsat and create a js date instance
var todaysDate=new Date('{!TODAY()}');
  // get today's date and create a js date instance
 /*
   Subtraction of date in js is subtraction in milliseconds of corresponding date   
   values. Hence the result is in milliseconds
  */
var numberOfDays = (todaysDate-creadtedDate)/(1000*60*60*24);
  //Converting milliseconds to days is to divide appropriatley

if(numberOfDays <= 21){

   window.parent.location.href = "/apex/Invoice_1?id"+'{!Opportunity.Id}';
}else{
    alert("Error: Opportunity is created "+numberOfDays+" before");
}

Hope this example helps

 

 

Pradeep_NavatarPradeep_Navatar

Try out the sample code given below :

 

if(({!Opportunity.CloseDate} - {!Opportunity.CreatedDate}) > 21)

{

 window.parent.location.href = "/apex/Invoice_1?id="+'{!Opportunity.Id}';

}

else

{

 //do nothing

}

 

Hope this helps.