web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

HOW TO COMPARE TWO DATES ?

(0) ShareShare
ReportReport
Posted on by 12,119 Moderator

Hi all,

I want to compare two dates one is coming from 'currentDateTime = new Date()' and the other is coming from retrieve through web api my whole code is given below:

function msdyn_customerasset_OnChange() {
Retrieve_msdyn_customerasset();
}
function Retrieve_msdyn_customerasset() {
var customerassetLookupId = Xrm.Page.getAttribute("msdyn_customerasset").getValue()[0].id,

fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
" <entity name='msdyn_customerasset'>" +
" <attribute name='msdyn_name' />" +
" <attribute name='msdyn_customerassetid' />" +
" <attribute name='aw_shutdownrequired' />" +
" <attribute name='aw_policyexpirationdate' />" +
" <order attribute='msdyn_name' descending='true' />" +
" <filter type='and'>" +
" <condition attribute='msdyn_customerassetid' operator='eq' value='" + customerassetLookupId + "' />" +
" </filter>" +
" </entity>" +
"</fetch>",
encodedFetchXml = encodeURI(fetchXml),
queryPath = "/api/data/v8.2/msdyn_customerassets?fetchXml=" + encodedFetchXml,//You must change the entity name including s and you must see the version of your web api as here v8.2
requestPath = Xrm.Page.context.getClientUrl() + queryPath,

req = new XMLHttpRequest();
req.open("GET", requestPath, false);//false for sychronous request
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
this.onreadystatechange = null;
if (this.status === 200) {
var returned = JSON.parse(this.responseText),
results = returned.value;
if (results.length == 1) {
setValuesof_msdyn_workorder(results);
}
} else {

alert(this.statusText);
}
}
};
req.send();
}

function setValuesof_msdyn_workorder(results) {//setValuesof_Service Order
var ShutdownRequired = results[0]["aw_shutdownrequired"];
if (ShutdownRequired != null && ShutdownRequired == true) {
Xrm.Page.getAttribute("aw_shutdownrequired").setValue(ShutdownRequired);
Xrm.Page.getControl("aw_shutdownstartdate").setVisible(true);
Xrm.Page.getControl("aw_shutdownenddate").setVisible(true);
} else if (ShutdownRequired != null && ShutdownRequired == false) {
Xrm.Page.getAttribute("aw_shutdownrequired").setValue(ShutdownRequired);
Xrm.Page.getControl("aw_shutdownstartdate").setVisible(false);
Xrm.Page.getControl("aw_shutdownenddate").setVisible(false);
}
var policyexpirationdate = results[0]["aw_policyexpirationdate"],

currentDateTime = new Date();
if (currentdate <= policyexpirationdate) {
Xrm.Page.getControl("aw_sendemailtovendor").setVisible(true);
Xrm.Page.getAttribute("aw_sendemailtovendor").setRequiredLevel("required");
} else {
Xrm.Page.getControl("aw_sendemailtovendor").setVisible(false);
Xrm.Page.getAttribute("aw_sendemailtovendor").setRequiredLevel("none");
}
}

Please do let me know. How can I compare two dates in this scenario?

Thanks

Regards,

AW

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Try changing as follows:

    // Add if policyexpirationdate has value logic here

    var policyexpirationdate = new Date(results[0]["aw_policyexpirationdate"]);

    currentDateTime = new Date();

    if (currentdate <= policyexpirationdate) ...

  • Abdul Wahab Profile Picture
    12,119 Moderator on at

    Thank you soo much Aric Levin

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.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans