Hi
How to compare a datetime field with a date field using javascript? Any suggestions?
*This post is locked for comments
Hi
How to compare a datetime field with a date field using javascript? Any suggestions?
*This post is locked for comments
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
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:
André Arnaud de Cal...
292,162
Super User 2025 Season 1
Martin Dráb
230,962
Most Valuable Professional
nmaenpaa
101,156