Notifications
Announcements
No record found.
Hi
How to compare a datetime field with a date field using javascript? Any suggestions?
*This post is locked for comments
Hi,
You can get the date part of the field by using the getFullYear, getMonth and getDate methods of the date field as follows:
var dateTimeField = Xrm.Page.getAttribute("bgx_startdate").getValue();
var yearValue = dateTimeField.getFullYear();
var monthValue = dateTimeField.getMonth(); // You will need to add 1 to get the current month as this is 0 based.
var dayValue = dateTimeField.getDate();
You can then get the date from these fields by calling the new Date function
var newDate = new Date(yearValue, motnhValue, dayValue);
You can then use standard logic to compare two date fields:
var dateField2 = Xrm.Page.getAttribute("bgx_enddate").getValue();
if (newDate > dateField2) ...
You can also use the dates.compare javascript function:
dates.compare(dateField1, dateField2)
This will return the following:
-1 if dateField1 < dateField2
0 if the dates are the same
1 if dateField1 > dateField2
See stackoverflow link below:
stackoverflow.com/.../497790
Hi Sathish, as pointed out by Aric, you will have to extract the date value from DateTime field then only it will be comparable.
Regards,
Pranav
Under review
Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
SA-08121319-0 4
Calum MacFarlane 4
Alex Fun Wei Jie 2