Sunday, 11 February 2018

Compare Two Date Field Values in Dynamics CRM using JavaScript

Compare CRM Date Field's Value from Today's Date:

function compareDateFromTodaysDate()
{
var startDate = Xrm.Page.getAttribute('birthdate').getValue();

var todayDate = new Date(); 
todayDate.setHours(0,0,0);

if(String(startDate) == String(todayDate)) 
{
alert("DOB is equal to today's date");
Xrm.Page.getAttribute('birthdate').setValue(null);
return;
}

if (startDate < todayDate) 
{
alert("DOB is less than today's date");
return;
}
if (startDate > todayDate) 
{
alert("DOB is greater than today's date");
Xrm.Page.getAttribute('birthdate').setValue(null);
return;
}
}


Compare CRM Two Date Field's Values:

function compareTwoDates()
{
        var DOB = Xrm.Page.getAttribute('birthdate').getValue();

var Anniversary = Xrm.Page.getAttribute('anniversary').getValue();

var DOBDate = new Date(DOB); 
 
        var AnniversaryDate = new Date(Anniversary);
       
if(String(DOBDate) == String(AnniversaryDate))
        {
            alert("DOB is equal to Anniversary Date ");
    return;
        }
   
if (DOBDate > AnniversaryDate) 
        {
            alert("DOB is greater then Anniversary Date ");
    return;
        }

        if (DOBDate < AnniversaryDate) 
        {
            alert("DOB is Less then Anniversary Date");
    return;
        }
        
}

Note: I have run both the functions on OnChange event of birthdate field of Contact Entity.


Cheers 😎

No comments:

Post a Comment

Blogger Widgets