Tags

, , , , ,

Hello Everyone,

In my previous blog, I talked about setting and clearing the FormNotifcations on the Dynamics CRM Forms. Here I’m going to talk about another useful script mostly used; Date Calculation.

Difference between 2 dates in Days:

 

function CalculateDays(Date1 , Date2) {
var OneDay = 1000*60*60*24; //The number of millisec in a day

var DateTime1 = new Date(Date1).getTime(); // convert date1 in milliseconds
var DateTime2 = new Date(Date2).getTime(); // convert date2 in milliseconds
var DateDiff = Math.abs(DateTime2 – DateTime1); //difference of dates in millisec

return Math.round(DateDiff / OneDay); //difference in days
}

 

Sample code to use this in for 2 CRM date fields:

var FutureDate= Xrm.Page.data.entity.attributes.get(“ashwin_futuredate”);

var PastDate= Xrm.Page.data.entity.attributes.get(“ashwin_pastdate”);

 

if (FutureDate.getValue() == null || PastDate.getValue() == null)

{

Xrm.Utility.alertDialog(“FutureDate or PastDate values should not be left blank!”,function(){return;});

}

 

else {

FutureDateVal= FutureDate.getValue(); // the fieldvalue returns datetime, so need to remove time

PastDateVal = PastDate.getValue();  // the fieldvalue returns datetime, so need to remove time

 

var year_f = FutureDateVal.getFullYear();

var month_f = FutureDateVal.getMonth();

var day_f = FutureDateVal.getDate();

var FutureDateValue = new Date(year_f, month_f, day_f);  //get only the date, time removed.

 

var year_p = PastDateVal.getFullYear();

var month_p = PastDateVal.getMonth();

var day_p = PastDateVal.getDate();

var PastDateValue = new Date(year_p, month_p, day_p);  //get only the date, time removed.

 

var Days = CalculateDays(PastDateValue, FutureDateValue);

//alert(Days);

 

}

 

I’m pinning this script useful for me in my upcoming projects. Hope this will be helpful for someone too.

Thanks ! 🙂

 

Advertisements