Adding Days/Months/Years to Date field in CRM using JavaScript

Requirement:

Adding Days / Months/ Years/ Hours to Date field in CRM.

By reading the requirement first thing comes in your mind is you can achieve above requirement by using Calculated fields.

Yes  you are right you can implement above requirement by configuring Calculated fields.

But you can create Calculated fields in Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online.

What if you are using Dynamics CRM 2011/CRM 2013/ CRM 2015?

You have achieve above requirement by using JavaScript.

Please find below JavaScript code for above requirement.

 

function OnchangeofDate1() {

var Date1 = Xrm.Page.getAttribute(“date1”).getValue(); // retriving Date time valie of Date1 field

var newdate = new Date(
Date1.getFullYear(), // If you want to add Years please add number of Years by adding “+” symbol.
Date1.getMonth(), // If you want to add Months please add number of Months by adding “+” symbol.
Date1.getDate() + 10, // If you want to add Days please add number of days by adding “+” symbol.  In my code I have added 10 days to Date1 field.
Date1.getHours(), // If you want to add Hours please add number of hours by adding “+” symbol.
Date1.getMinutes()); // If you want to add Minutes please add number of Minutes by adding “+” symbol.

Xrm.Page.getAttribute(“date2”).setValue(newdate); //Setting new Data time(after adding Days/Months/Years) to date2 field.

}

 

Hope this helps you 🙂

 

 

Leave a comment