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 😎

Published by arpitpowerguide

My name is Arpit Shrivastava, who is a Microsoft MVP in the Business Applications category. I am a Microsoft Dynamics 365 and Power Platform enthusiast person who is having a passion for researching and learning new things and acquiring immense knowledge. I am providing consistent help, support, and sharing my knowledge through various Social Media Channels along with my Personal Blog, Microsoft Community, conducting online training and attending various 365 Saturday Events worldwide and sharing the best Solutions to the readers helping them achieve their goals and objectives in Customer Relationship Space.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: